musl_template.gni 13.6 KB
Newer Older
W
weichaox 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
import("//build/ohos.gni")
import("musl_src.gni")

template("musl_libs") {
  no_default_deps = true

  forward_variables_from(invoker, [ "target_abi" ])
  abi_prefix = target_abi

  if (target_abi == "soft") {
    _libs_path_prefix = "."
    _libs_out_dir = "usr/lib/${musl_target_triple}"
  } else {
    _libs_path_prefix = "${target_abi}"
    _libs_out_dir = "usr/lib/${musl_target_triple}/${target_abi}"
  }

  musl_subarch = ""
  if (target_abi == "a7_hard_neon-vfpv4") {
    musl_subarch = "hf"
  }

  porting_deps = [
    "//third_party/musl:create_alltypes_h",
    "//third_party/musl:create_porting_src",
    "//third_party/musl:create_syscall_h",
    "//third_party/musl:create_version_h",
  ]

  group(target_name) {
    deps = [
      ":${abi_prefix}_create_linker",
      ":${abi_prefix}_libc_musl_shared",
      ":${abi_prefix}_libc_musl_static",
      ":${abi_prefix}_musl_crt_libs",
    ]
  }

  group("${abi_prefix}_shared_libs") {
    deps = [
      ":${abi_prefix}_libc_musl_shared",
      ":${abi_prefix}_libcrypt",
      ":${abi_prefix}_libdl",
      ":${abi_prefix}_libm",
      ":${abi_prefix}_libpthread",
      ":${abi_prefix}_libresolv",
      ":${abi_prefix}_librt",
      ":${abi_prefix}_libutil",
      ":${abi_prefix}_libxnet",
      ":${abi_prefix}_musl_crt_libs",
      ":musl_headers",
    ]
  }

  group("${abi_prefix}_static_libs") {
    deps = [
      ":${abi_prefix}_libc_musl_static",
      ":${abi_prefix}_libcrypt",
      ":${abi_prefix}_libdl",
      ":${abi_prefix}_libm",
      ":${abi_prefix}_libpthread",
      ":${abi_prefix}_libresolv",
      ":${abi_prefix}_librt",
      ":${abi_prefix}_libutil",
      ":${abi_prefix}_libxnet",
      ":${abi_prefix}_musl_crt_libs",
      ":musl_headers",
    ]
  }

  group("${abi_prefix}_musl_crt_libs") {
    deps = [ ":${abi_prefix}_musl_crt_install_action" ]
  }

  # part of default_compiler_configs from build/config/BUILDCONFIG.gn
  musl_inherited_configs = [
    "//build/config/compiler:afdo",
    "//build/config/compiler:afdo_optimize_size",
    "//build/config/compiler:compiler",
    "//build/config/compiler:compiler_arm_fpu",
    "//build/config/compiler:compiler_arm_thumb",
    "//build/config/compiler:chromium_code",
    "//build/config/compiler:default_include_dirs",
    "//build/config/compiler:default_optimization",
    "//build/config/compiler:default_stack_frames",
    "//build/config/compiler:default_symbols",
    "//build/config/compiler:export_dynamic",
    "//build/config/compiler:no_exceptions",
    "//build/config/compiler:no_rtti",
    "//build/config/compiler:runtime_library",
    "//build/config/compiler:thin_archive",
D
dhy308 已提交
92
    "//build/config/sanitizers:default_sanitizer_flags",
W
weichaox 已提交
93 94 95
  ]

  config("${abi_prefix}_musl_abi_config") {
Z
zhuoli 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    if (musl_arch == "arm") {
      if (target_abi == "soft") {
        cflags = [ "-mfloat-abi=soft" ]
      } else if (target_abi == "a7_soft") {
        cflags = [
          "-mcpu=cortex-a7",
          "-mfloat-abi=soft",
        ]
      } else if (target_abi == "a7_softfp_neon-vfpv4") {
        cflags = [
          "-mcpu=cortex-a7",
          "-mfloat-abi=softfp",
          "-mfpu=neon-vfpv4",
        ]
      } else if (target_abi == "a7_hard_neon-vfpv4") {
        cflags = [
          "-mcpu=cortex-a7",
          "-mfloat-abi=hard",
          "-mfpu=neon-vfpv4",
        ]
      }
W
weichaox 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    }
  }

  config("${abi_prefix}_musl_config") {
    configs = [ ":${abi_prefix}_musl_abi_config" ]

    include_dirs = [
      "${target_out_dir}/${musl_ported_dir}/arch/${musl_arch}",
      "${target_out_dir}/${musl_ported_dir}/arch/generic",
      "${target_out_dir}/${musl_ported_dir}/src/internal",
      "${target_out_dir}/${musl_ported_dir}/src/include",
      "${target_out_dir}/${musl_ported_dir}/include",
      "${target_out_dir}/${musl_inc_out_dir}",
    ]

    cflags_basic = [
      "--target=${musl_target_triple}",
      "-Wall",
      "-D_FORTIFY_SOURCE=2",
      "-Wl,-z,relro,-z,now,-z,noexecstack",
    ]

Z
zhuoli 已提交
139
    if (musl_arch == "arm") {
D
dhy308 已提交
140
      cflags_basic += [ "-march=armv7-a" ]
D
dhy308 已提交
141
      cflags_basic += [ "-mtp=cp15" ]
Z
zhuoli 已提交
142
    } else if (musl_arch == "aarch64") {
D
dhy308 已提交
143
      cflags_basic += [ "-march=armv8" ]
Z
zhuoli 已提交
144 145
    }

W
weichaox 已提交
146 147
    cflags_auto = [
      "-D_XOPEN_SOURCE=700",
D
dhy308 已提交
148
      "-g",
W
weichaox 已提交
149
      "-pipe",
D
dhy308 已提交
150 151 152
      "-fno-omit-frame-pointer",
      "-funwind-tables",
      "-fasynchronous-unwind-tables",
W
weichaox 已提交
153 154 155 156 157 158 159 160 161
      "-ffunction-sections",
      "-fdata-sections",
      "-Werror=implicit-function-declaration",
      "-Werror=implicit-int",
      "-Werror=pointer-sign",
      "-Werror=pointer-arith",
      "-Qunused-arguments",
    ]

M
mucor 已提交
162 163 164 165
    if (!is_asan) {
      cflags_auto += [ "-DHOOK_ENABLE" ]
    }

166 167
    cflags_auto += [ "-DRESERVE_SIGNAL_STACK" ]

W
weichaox 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    cflags_c99fse = [
      "-std=c99",
      "-nostdinc",
      "-ffreestanding",
      "-frounding-math",
      "-Wa,--noexecstack",
    ]

    cflags_all = cflags_basic + cflags_c99fse + cflags_auto

    cflags = cflags_all

    if ("${target_abi}" != "a7_hard_neon-vfpv4") {
      defines = [ "BROKEN_VFP_ASM" ]
    }

    dynamic_list =
        rebase_path("${target_out_dir}/${musl_ported_dir}/dynamic.list")

    ldflags = cflags_all
    ldflags += [
      "--target=${musl_target_triple}",
      "-fuse-ld=lld",
      "-Wl,--sort-section,alignment",
      "-Wl,--sort-common",
      "-Wl,--gc-sections",
      "-Wl,--hash-style=both",
      "-Wl,--no-undefined",
      "-Wl,--exclude-libs=ALL",
      "-Wl,--dynamic-list=${dynamic_list}",
    ]
    asmflags = cflags
  }

chuxuezhe1111's avatar
chuxuezhe1111 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  config("${abi_prefix}_hook") {
    defines = []
    configs = []

    if (is_posix) {
      configs += [ "//build/config/posix:runtime_library" ]
    }

    cflags_cc = []

    defines = [
      "__GNU_SOURCE=1",  # Necessary for clone().
      "CHROMIUM_CXX_TWEAK_INLINES",  # Saves binary size.
    ]

    defines += [
      "__MUSL__",
      "_LIBCPP_HAS_MUSL_LIBC",
      "__BUILD_LINUX_WITH_CLANG",
    ]
M
mucor 已提交
222 223 224 225

    if (!is_asan) {
      defines += [ "HOOK_ENABLE" ]
    }
chuxuezhe1111's avatar
chuxuezhe1111 已提交
226 227 228 229 230 231 232 233 234
    ldflags = [ "-nostdlib" ]

    libs = []

    if (is_component_build) {
      defines += [ "COMPONENT_BUILD" ]
    }
  }

W
weichaox 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  source_set("${abi_prefix}_musl_crt") {
    sources = [
      "${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/crti.s",
      "${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/crtn.s",
      "${target_out_dir}/${musl_ported_dir}/crt/Scrt1.c",
      "${target_out_dir}/${musl_ported_dir}/crt/crt1.c",
      "${target_out_dir}/${musl_ported_dir}/crt/rcrt1.c",
    ]

    defines = [ "CRT" ]

    configs -= musl_inherited_configs
    configs += [ ":${abi_prefix}_musl_config" ]
    cflags = [
      "-fPIC",
      "-fno-stack-protector",
    ]

    deps = porting_deps

    asmflags = cflags
  }

  source_set("${abi_prefix}_musl_src") {
    sources_orig = []
    sources = []
    set_sources_assignment_filter([])

    sources_orig = musl_src_arch_file + musl_src_file
    sources_orig -= musl_src_filterout
    sources_orig -= [
      "src/string/mempcpy.c",
      "src/string/memset.c",
      "src/env/__init_tls.c",
      "src/env/__libc_start_main.c",
      "src/env/__stack_chk_fail.c",
    ]

Z
zhuoli 已提交
273 274 275 276 277 278
    if (musl_arch == "arm") {
      sources_orig -= [ "src/thread/${musl_arch}/__set_thread_area.c" ]
    } else if (musl_arch == "aarch64") {
      sources_orig -= [ "src/thread/${musl_arch}/__set_thread_area.s" ]
    }

W
weichaox 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    if ("${target_abi}" == "a7_hard_neon-vfpv4") {
      defines = [ "__ARM_PCS_VFP" ]
    }

    foreach(s, sources_orig) {
      sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ]
    }

    cflags = [
      "-O3",
      "-fPIC",
      "-fstack-protector-strong",
    ]

    configs -= musl_inherited_configs
    configs += [ ":${abi_prefix}_musl_config" ]

    deps = porting_deps
  }

  source_set("${abi_prefix}_musl_src_nossp") {
    sources = []
    sources_orig = [
      "src/string/mempcpy.c",
      "src/string/memset.c",
      "src/env/__init_tls.c",
      "src/env/__libc_start_main.c",
      "src/env/__stack_chk_fail.c",
    ]

Z
zhuoli 已提交
309 310 311 312 313 314
    if (musl_arch == "arm") {
      sources_orig += [ "src/thread/${musl_arch}/__set_thread_area.c" ]
    } else if (musl_arch == "aarch64") {
      sources_orig += [ "src/thread/${musl_arch}/__set_thread_area.s" ]
    }

W
weichaox 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    foreach(s, sources_orig) {
      sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ]
    }

    configs -= musl_inherited_configs
    configs += [ ":${abi_prefix}_musl_config" ]
    cflags = [
      "-O3",
      "-fPIC",
      "-fno-stack-protector",
    ]

    deps = porting_deps
  }

  source_set("${abi_prefix}_musl_ldso") {
    sources = []
    sources_orig = musl_src_ldso

    foreach(s, sources_orig) {
      sources += [ "${target_out_dir}/${musl_ported_dir}/${s}" ]
    }

    configs -= musl_inherited_configs
    configs += [ ":${abi_prefix}_musl_config" ]
    cflags = [
      "-fPIC",
      "-fno-stack-protector",
    ]

    deps = porting_deps
  }

chuxuezhe1111's avatar
chuxuezhe1111 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361
  source_set("${abi_prefix}_musl_hook") {
    sources = [
      "./porting/linux/user/src/hook/malloc_common.c",
      "./porting/linux/user/src/hook/musl_preinit.c",
      "./porting/linux/user/src/hook/musl_preinit_common.c",
    ]

    deps = [
      "//third_party/musl:create_alltypes_h",
      "//third_party/musl:create_porting_src",
      "//third_party/musl:create_syscall_h",
      "//third_party/musl:create_version_h",
      "//third_party/musl:musl_copy_inc_bits",
      "//third_party/musl:musl_copy_inc_root",
C
chenjianwen 已提交
362
      "//third_party/musl:musl_copy_inc_sys",
chuxuezhe1111's avatar
chuxuezhe1111 已提交
363 364 365 366 367 368 369 370 371 372
    ]

    configs -= musl_inherited_configs

    configs += [
      "//build/config/compiler:compiler",
      ":${abi_prefix}_hook",
    ]
  }

W
weichaox 已提交
373 374 375 376 377 378 379 380
  static_library("${abi_prefix}_libc_musl_static") {
    output_name = "libc"
    complete_static_lib = true

    configs -= musl_inherited_configs

    output_dir = "${target_out_dir}/${_libs_out_dir}"
    deps = [
M
maweiye 已提交
381
      ":${abi_prefix}_musl_hook",
W
weichaox 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
      ":${abi_prefix}_musl_src",
      ":${abi_prefix}_musl_src_nossp",
    ]
  }

  static_library("${abi_prefix}_libm") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "libm"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  static_library("${abi_prefix}_librt") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "librt"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  static_library("${abi_prefix}_libpthread") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "libpthread"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  static_library("${abi_prefix}_libcrypt") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "libcrypt"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  static_library("${abi_prefix}_libutil") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "libutil"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  static_library("${abi_prefix}_libresolv") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "libresolv"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  static_library("${abi_prefix}_libxnet") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "libxnet"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  static_library("${abi_prefix}_libdl") {
    complete_static_lib = true
    configs -= musl_inherited_configs
    output_name = "libdl"
    output_dir = "${target_out_dir}/${_libs_out_dir}"
  }

  shared_library("${abi_prefix}_libc_musl_shared") {
    output_dir = "${target_out_dir}/${_libs_out_dir}"
    output_name = "libc"
    output_extension = "so"

    libclang_rt_path = rebase_path(
            "${runtime_lib_path}/${musl_target_triple}/${_libs_path_prefix}")

D
dhy308 已提交
451 452
    musl_lib_path = rebase_path("${target_out_dir}/${_libs_out_dir}")

W
weichaox 已提交
453 454 455 456 457
    ldflags = [
      "-nostdlib",
      "-Wl,-e,_dlstart",
      "-L${libclang_rt_path}",
      "-lclang_rt.builtins",
D
dhy308 已提交
458 459 460 461
      "-lunwind",
      "-L${musl_lib_path}",
      "-ldl",
      "-lpthread",
W
weichaox 已提交
462 463 464 465 466 467 468
    ]

    configs -= musl_inherited_configs
    configs -= [ "//build/config:default_libs" ]
    configs += [ ":${abi_prefix}_musl_config" ]

    deps = [
D
dhy308 已提交
469 470
      ":${abi_prefix}_libdl",
      ":${abi_prefix}_libpthread",
471
      ":${abi_prefix}_musl_crt_install_action",
chuxuezhe1111's avatar
chuxuezhe1111 已提交
472
      ":${abi_prefix}_musl_hook",
W
weichaox 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      ":${abi_prefix}_musl_ldso",
      ":${abi_prefix}_musl_src",
      ":${abi_prefix}_musl_src_nossp",
    ]
  }

  action_foreach("${abi_prefix}_musl_crt_install_action") {
    redir = "${root_out_dir}/obj"
    script = "${target_out_dir}/${musl_ported_dir}/scripts/install.py"
    sources = [
      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/${abi_prefix}_musl_crt/Scrt1.o",
      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/${abi_prefix}_musl_crt/crt1.o",
      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/${abi_prefix}_musl_crt/rcrt1.o",
      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/${abi_prefix}_musl_crt/crti.o",
      "${redir}/${target_out_dir}/${musl_ported_dir}/crt/${musl_arch}/${abi_prefix}_musl_crt/crtn.o",
    ]
    outputs = [ "${target_out_dir}/${_libs_out_dir}/{{source_file_part}}" ]
    args = [
      "--input",
      "{{source}}",
    ]
    args += [ "--output" ] + rebase_path(outputs, root_build_dir)
    deps = [ ":${abi_prefix}_musl_crt" ]
  }

  copy("${abi_prefix}_create_linker") {
    deps = [ ":${abi_prefix}_libc_musl_shared" ]

    # _libc_shared_outputs = get_target_outputs(":${abi_prefix}_libc_musl_shared")
    _libc_shared_so = "${target_out_dir}/${_libs_out_dir}/libc.so"
    sources = [ _libc_shared_so ]
C
Caoruihong 已提交
504 505 506 507 508 509
    if (is_asan) {
      asan = "-asan"
    } else {
      asan = ""
    }
    _muls_linker_so = "${root_out_dir}/common/common/libc/${_libs_path_prefix}/ld-musl-${musl_arch}${musl_subarch}${asan}.so.1"
W
weichaox 已提交
510 511 512
    outputs = [ _muls_linker_so ]
  }
}