Home | History | Annotate | Download | only in toolchain
      1 # Copyright (C) 2017 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 import("//gn/standalone/android.gni")
     16 import("//gn/standalone/wasm.gni")
     17 import("llvm.gni")
     18 
     19 declare_args() {
     20   if (is_clang) {
     21     if (is_system_compiler) {
     22       cc = "\$CC "
     23       cxx = "\$CXX "
     24     } else if (is_linux) {
     25       cc = linux_clang_bin
     26       cxx = linux_clangxx_bin
     27     } else {
     28       cc = "clang"
     29       cxx = "clang++"
     30     }
     31   } else {
     32     cc = "gcc"
     33     cxx = "g++"
     34   }
     35 }
     36 
     37 declare_args() {
     38   host_ar = ar
     39   if (is_linux_host && is_clang) {
     40     host_cc = linux_clang_bin
     41     host_cxx = linux_clangxx_bin
     42   } else {
     43     host_cc = cc
     44     host_cxx = cxx
     45   }
     46 
     47   if (is_android) {
     48     target_ar = "$android_toolchain_root/bin/$android_abi_target-ar"
     49     target_cc = "$android_llvm_dir/bin/clang"
     50     target_cxx = "$android_llvm_dir/bin/clang++"
     51   } else {
     52     target_ar = ar
     53     target_cc = cc
     54     target_cxx = cxx
     55   }
     56   cc_wrapper = ""
     57 }
     58 
     59 python = "python"
     60 stamp = "touch"
     61 
     62 template("gcc_like_toolchain") {
     63   toolchain(target_name) {
     64     ar = invoker.ar
     65     cc = invoker.cc
     66     cxx = invoker.cxx
     67     lib_switch = "-l"
     68     lib_dir_switch = "-L"
     69 
     70     tool("cc") {
     71       depfile = "{{output}}.d"
     72       command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} ${extra_cflags} -c {{source}} -o {{output}}"
     73       depsformat = "gcc"
     74       outputs = [
     75         "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
     76       ]
     77       description = "compile {{source}}"
     78     }
     79 
     80     tool("cxx") {
     81       depfile = "{{output}}.d"
     82       command = "$cc_wrapper $cxx -MMD -MF $depfile {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}}  ${extra_cflags} ${extra_cxxflags} -c {{source}} -o {{output}}"
     83       depsformat = "gcc"
     84       outputs = [
     85         "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
     86       ]
     87       description = "compile {{source}}"
     88     }
     89 
     90     tool("asm") {
     91       depfile = "{{output}}.d"
     92       command = "$cc_wrapper $cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
     93       depsformat = "gcc"
     94       outputs = [
     95         "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o",
     96       ]
     97       description = "assemble {{source}}"
     98     }
     99 
    100     tool("alink") {
    101       if (is_mac && ar != "suppress_unused_ar_variable_warning") {
    102         command = "rm -f {{output}} && libtool -static {{arflags}} -o {{output}} {{inputs}}"
    103       } else {
    104         rspfile = "{{output}}.rsp"
    105         rspfile_content = "{{inputs}}"
    106         command = "$ar rcsD {{output}} @$rspfile"
    107       }
    108       outputs = [
    109         "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
    110       ]
    111       default_output_extension = ".a"
    112       output_prefix = "lib"
    113       description = "link {{output}}"
    114     }
    115 
    116     tool("solink") {
    117       soname = "{{target_output_name}}{{output_extension}}"
    118 
    119       rpath = "-Wl,-soname,$soname"
    120       if (is_mac) {
    121         rpath = "-Wl,-install_name,@rpath/$soname"
    122       }
    123 
    124       command = "$cc_wrapper $cxx -shared {{ldflags}} ${extra_ldflags} {{inputs}} {{solibs}} {{libs}} $rpath -o {{output}}"
    125       outputs = [
    126         "{{root_out_dir}}/$soname",
    127       ]
    128       output_prefix = "lib"
    129       default_output_extension = ".so"
    130       description = "link {{output}}"
    131     }
    132 
    133     tool("link") {
    134       command = "$cc_wrapper $cxx {{ldflags}} ${extra_ldflags} {{inputs}} {{solibs}} {{libs}} -o {{output}}"
    135       outputs = [
    136         "{{root_out_dir}}/{{target_output_name}}{{output_extension}}",
    137       ]
    138       description = "link {{output}}"
    139     }
    140 
    141     tool("stamp") {
    142       command = "touch {{output}}"
    143       description = "stamp {{output}}"
    144     }
    145 
    146     tool("copy") {
    147       command = "cp -af {{source}} {{output}}"
    148       description = "COPY {{source}} {{output}}"
    149     }
    150 
    151     toolchain_args = {
    152       current_cpu = invoker.cpu
    153       current_os = invoker.os
    154     }
    155   }
    156 }
    157 
    158 gcc_like_toolchain("gcc_like") {
    159   cpu = current_cpu
    160   os = current_os
    161   ar = target_ar
    162   cc = target_cc
    163   cxx = target_cxx
    164 }
    165 
    166 gcc_like_toolchain("gcc_like_host") {
    167   cpu = host_cpu
    168   os = host_os
    169   ar = host_ar
    170   cc = host_cc
    171   cxx = host_cxx
    172 }
    173 
    174 gcc_like_toolchain("wasm") {
    175   # emsdk_dir and em_config are defined in wasm.gni.
    176   cpu = host_cpu
    177   os = host_os
    178   ar = "$emsdk_dir/emscripten/emar --em-config $em_config"
    179   cc = "$emsdk_dir/emscripten/emcc --em-config $em_config"
    180   cxx = "$emsdk_dir/emscripten/em++ --em-config $em_config"
    181 }
    182