Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a custom toolchain for customized use #8077

Merged
merged 4 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build/config/BUILDCONFIG.gn
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ if (host_toolchain == "") {

if (_chip_defaults.custom_toolchain != "") {
_default_toolchain = _chip_defaults.custom_toolchain
} else if (target_os == "linux" &&
(target_cpu == "arm64" || target_cpu == "arm")) {
_default_toolchain = "${_build_overrides.build_root}/toolchain/linux_arm"
} else if (target_os == host_os) {
_default_toolchain = host_toolchain
} else if (target_os == "all") {
Expand Down
21 changes: 21 additions & 0 deletions build/toolchain/linux_arm/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2020 The Pigweed Authors
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("linux_arm_toolchain.gni")

linux_arm_toolchain("linux_arm") {
toolchain_args = {
}
}
45 changes: 45 additions & 0 deletions build/toolchain/linux_arm/linux_arm_toolchain.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")

import("${build_root}/config/android/config.gni")
import("${build_root}/toolchain/gcc_toolchain.gni")

declare_args() {
# similar to "CROSS_COMPILE" when build kernel
linux_arm_cross_compile = ""
}

template("linux_arm_toolchain") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing ARM specific about this. I think you are looking for just a customizable toolchain.

if (linux_arm_cross_compile == "") {
assert(
false,
"assign a value to the build argument 'linux_arm_cross_compile' when use linux_arm_toolchain")
}

_invoker_toolchain_args = invoker.toolchain_args
_linux_arm_toolchain_args = {
current_os = invoker.current_os
is_clang = false
forward_variables_from(_invoker_toolchain_args, "*")
}

gcc_toolchain(target_name) {
toolchain_args = _linux_arm_toolchain_args
ar = linux_arm_cross_compile + "ar"
Copy link
Contributor

@mspang mspang Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand <target-triple>-<tool> is a common tool naming convention for gcc & binutils but I don't think it is necessarily a good idea to assume it. Could you make separate arguments for each tool instead?

E.g.

# build/toolchain/custom/BUILD.gn ?

import("//build_overrides/build.gni")
import("${build_root}/toolchain/gcc_toolchain.gni")
import("${build_root}/config/compiler/compiler.gni")

declare_args() {
  # C compiler executable to use for target build.
  target_cc = ""

  # C++ compiler to use for target build.
  target_cxx = ""

  # Archive tool to use for target build.
  target_ar = ""
}

gcc_toolchain("target") {
  ar = target_ar
  cc = target_cc
  cxx = target_cxx

  toolchain_args = {
    current_os = target_os
    current_cpu = target_cpu
    is_clang = is_clang
  }
}

Also, could you maybe just check that definition into the repository somewhere and point custom_toolchain at it to use it?

  gn gen out/custom --args="
    custom_toolchain=\"//build/toolchain/custom:target\"
    target_cc = \"${CROSS_COMPILE}-gcc\"
    target_cxx = \"${CROSS_COMPILE}-g++\"
    target_cxx = \"${CROSS_COMPILE}-ar\"
  "

The current modifications to BUILDCONFIG.gn activate for any Linux ARM config even if you are running on a Linux ARM host and you are not cross compiling, so your compiler is really just "gcc".

Copy link
Contributor Author

@FaqiangZhu-nxp FaqiangZhu-nxp Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mspang
it's a good idea to make separate arguments for each tool.

For your second comment, in fact, I used to specify the toolchain with build argument "custom_toolchain", and then found it a bit unease of use:

  1. when I use gn at the top direcory of connectedhomeip, I specify the custom toolchain like: custom_toolchain="//build/toolchain/linux_arm:linux_arm"
  2. when I use gn at a subdirectory like "examples/lighting-app/linux/", I specify the custom toolchain like: custom_toolchain="//third_party/connectedhomeip/build/toolchain/linux_arm:linux_arm"

Different string is used to specify the same toolchain, For me this is totally okay, but I think that this may be a bit confusing for customers who are beginners of gn tools. Can I made the change to "build/config/BUILDCONFIG.gn" like below?
else if (target_os == "linux" && (target_cpu == "arm64" || target_cpu == "arm") && target_cpu != host_cpu) { _default_toolchain = "${_build_overrides.build_root}/toolchain/linux_arm" }

Copy link
Contributor

@mspang mspang Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mspang
it's a good idea to make separate arguments for each tool.

For your second comment, in fact, I used to specify the toolchain with build argument "custom_toolchain", and then found it a bit unease of use:

  1. when I use gn at the top direcory of connectedhomeip, I specify the custom toolchain like: custom_toolchain="//build/toolchain/linux_arm:linux_arm"
  2. when I use gn at a subdirectory like "examples/lighting-app/linux/", I specify the custom toolchain like: custom_toolchain="//third_party/connectedhomeip/build/toolchain/linux_arm:linux_arm"

Different string is used to specify the same toolchain, For me this is totally okay, but I think that this may be a bit confusing for customers who are beginners of gn tools.

What about

gn gen out/custom --args="
    import(\"//build_overrides/build.gni\")
    custom_toolchain=\"${build_root}/toolchain/linux_arm:linux_arm\"
    target_cc = \"${CROSS_COMPILE}-gcc\"
    target_cxx = \"${CROSS_COMPILE}-g++\"
    target_cxx = \"${CROSS_COMPILE}-ar\"
"

Otherwise I think defining a new argument and doing something like

gn gen out/custom --args="
    use_custom_target_toolchain = true
    target_cc = \"${CROSS_COMPILE}-gcc\"
    target_cxx = \"${CROSS_COMPILE}-g++\"
    target_cxx = \"${CROSS_COMPILE}-ar\"
"

would be fine.

Can I made the change to "build/config/BUILDCONFIG.gn" like below?
else if (target_os == "linux" && (target_cpu == "arm64" || target_cpu == "arm") && target_cpu != host_cpu) { _default_toolchain = "${_build_overrides.build_root}/toolchain/linux_arm" }

I don't think we should couple providing a toolchain to cross compiling on Linux. Linux ARM[64] is a very common target, and we already need to support it well for Raspberry Pi. This should be made to work by using the compiler that running the "bootstrap" process provides you:

gn gen out/arm --args='target_cpu="arm" is_clang=true'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @mspang , I've updated the PR based on your comments.

cc = linux_arm_cross_compile + "gcc"
cxx = linux_arm_cross_compile + "g++"
}
}