1# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
2# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without modification,
5# are permitted provided that the following conditions are met:
6#
7# 1. Redistributions of source code must retain the above copyright notice, this list of
8#    conditions and the following disclaimer.
9#
10# 2. Redistributions in binary form must reproduce the above copyright notice, this list
11#    of conditions and the following disclaimer in the documentation and/or other materials
12#    provided with the distribution.
13#
14# 3. Neither the name of the copyright holder nor the names of its contributors may be used
15#    to endorse or promote products derived from this software without specific prior written
16#    permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import("//build/lite/config/component/lite_component.gni")
31
32targets = ""
33if (ohos_kernel_type == "liteos_a") {
34  targets = "liteos_a_user"
35} else if (ohos_kernel_type == "linux") {
36  targets = "linux_user"
37}
38
39assert(targets != "", "Unsupported ohos_kernel_type: $ohos_kernel_type")
40
41sysroot_path = rebase_path(ohos_current_sysroot)
42arch_cflags = string_join(" ", target_arch_cflags)
43
44build_ext_component("build_sysroot") {
45  no_default_deps = true
46  deps = []
47  exec_path = rebase_path(target_out_dir)
48  if (!defined(board_configed_sysroot) || board_configed_sysroot == "") {
49    makefile = rebase_path("Makefile", exec_path)
50    command = "make TOPDIR=$ohos_root_path SYSROOTDIR=$sysroot_path TARGETS=$targets -f $makefile"
51    command += " ARCH=$arch ARCH_CFLAGS=\"$arch_cflags\""
52    if (ohos_build_compiler == "clang") {
53      command += " CLANG=\"$ohos_current_cc_command\" TARGET=$target_triple"
54    } else {
55      command += " GCC=\"$ohos_current_cc_command\""
56    }
57    if (ohos_build_type == "debug" && !is_llvm_build) {
58      command += " BUILD_DEBUG=true"
59    }
60    if (ohos_kernel_type == "linux") {
61      command += " LINUXDIR=" + rebase_path("$root_out_dir/kernel/linux-4.19")
62      command += " PREBUILTLINUXHDRDIR=" + rebase_path(
63                     "//kernel/linux/patches/linux-4.19/prebuilts/usr/include")
64    }
65  } else {
66    command = "true"
67  }
68
69  if (!is_llvm_build) {
70    # copy C/C++ runtime libraries to lib out dir
71    if (ohos_build_compiler == "clang") {
72      runtime_libs = [
73        "libc++.so",
74        "libc.so",
75      ]
76      compiler_cmd = "$ohos_current_cxx_command --target=$target_triple --sysroot=$sysroot_path $arch_cflags"
77    } else {
78      runtime_libs = [
79        "libstdc++.so.6",
80        "libc.so",
81        "libgcc_s.so.1",
82      ]
83      compiler_cmd =
84          "$ohos_current_cxx_command --sysroot=$sysroot_path $arch_cflags"
85    }
86
87    libs = []
88    outputs = []
89    foreach(lib, runtime_libs) {
90      libs += [ "\$($compiler_cmd -print-file-name=$lib)" ]
91      outputs += [ "$root_out_dir/unstripped/usr/lib/$lib" ]
92    }
93
94    lib_out_dir = rebase_path("$root_out_dir/unstripped/usr/lib", exec_path)
95    command += " && mkdir -p $lib_out_dir && sh -c \"cp -f " +
96               string_join(" ", libs) + " $lib_out_dir\""
97  }
98}
99
100build_ext_component("build_sysroot_header") {
101  no_default_deps = true
102  deps = []
103  exec_path = rebase_path(target_out_dir)
104  if (!defined(board_configed_sysroot) || board_configed_sysroot == "") {
105    makefile = rebase_path("Makefile", exec_path)
106    command = "make musl_header_install_for_liteos_a_user TOPDIR=$ohos_root_path SYSROOTDIR=$sysroot_path TARGETS=$targets -f $makefile"
107    command += " ARCH=$arch ARCH_CFLAGS=\"$arch_cflags\""
108    if (ohos_build_compiler == "clang") {
109      command += " CLANG=\"$ohos_current_cc_command\" TARGET=$target_triple"
110    } else {
111      command += " GCC=\"$ohos_current_cc_command\""
112    }
113    if (ohos_build_type == "debug" && !is_llvm_build) {
114      command += " BUILD_DEBUG=true"
115    }
116    if (ohos_kernel_type == "linux") {
117      deps += [ "//kernel/linux/build:linux_kernel" ]
118      command += " LINUXDIR=" + rebase_path("$root_out_dir/kernel/linux-4.19")
119      command += " PREBUILTLINUXHDRDIR=" + rebase_path(
120                     "//kernel/linux/patches/linux-4.19/prebuilts/usr/include")
121    }
122  } else {
123    command = "true"
124  }
125}
126
127if (!is_llvm_build) {
128  action_foreach("strip") {
129    no_default_deps = true
130    deps = [ ":build_sysroot" ]
131    script = "//build/lite/run_shell_cmd.py"
132    sources_raw = get_target_outputs(deps[0])
133    sources = filter_exclude(sources_raw, [ "*.txt" ])
134    outputs = [ "$root_out_dir/libs/{{source_file_part}}" ]
135    args = [
136      "$ohos_current_strip_command",
137      "{{source}}",
138      "-o",
139      rebase_path("$root_out_dir/libs/{{source_file_part}}", root_build_dir),
140    ]
141  }
142}
143
144config("sysroot_flags") {
145  if (ohos_build_compiler == "clang") {
146    cflags = [
147      "--target=$target_triple",
148      "--sysroot=$sysroot_path",
149    ]
150  } else {
151    cflags = [
152      "--sysroot=$sysroot_path",
153      "-specs=musl-gcc.specs",
154    ]
155  }
156  cflags_cc = cflags
157  ldflags = cflags
158  asmflags = cflags
159}
160
161group("build") {
162  all_dependent_configs = [ ":sysroot_flags" ]
163  deps = [ ":build_sysroot" ]
164  if (!is_llvm_build) {
165    deps += [ ":strip" ]
166  }
167}
168