|
| 1 | +# swift_build_support/products/wasisysroot.py -------------------*- python -*- |
| 2 | +# |
| 3 | +# This source file is part of the Swift.org open source project |
| 4 | +# |
| 5 | +# Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +# |
| 8 | +# See https://swift.org/LICENSE.txt for license information |
| 9 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +# |
| 11 | +# ---------------------------------------------------------------------------- |
| 12 | + |
| 13 | +import multiprocessing |
| 14 | +import os |
| 15 | + |
| 16 | +from . import cmake_product |
| 17 | +from . import llvm |
| 18 | +from . import product |
| 19 | +from .. import shell |
| 20 | + |
| 21 | + |
| 22 | +class WASILibc(product.Product): |
| 23 | + @classmethod |
| 24 | + def product_source_name(cls): |
| 25 | + return "wasi-libc" |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def is_build_script_impl_product(cls): |
| 29 | + return False |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def is_before_build_script_impl_product(cls): |
| 33 | + return False |
| 34 | + |
| 35 | + def should_build(self, host_target): |
| 36 | + # wasi-libc should always be built if standard library is being built for |
| 37 | + # WebAssembly. |
| 38 | + return self.args.build_wasmstdlib |
| 39 | + |
| 40 | + def should_test(self, host_target): |
| 41 | + return False |
| 42 | + |
| 43 | + def should_install(self, host_target): |
| 44 | + return False |
| 45 | + |
| 46 | + def build(self, host_target): |
| 47 | + build_root = os.path.dirname(self.build_dir) |
| 48 | + llvm_build_dir = os.path.join('..', build_root, '%s-%s' % ('llvm', host_target)) |
| 49 | + build_jobs = self.args.build_jobs or multiprocessing.cpu_count() |
| 50 | + |
| 51 | + sysroot_build_dir = WASILibc.sysroot_build_path(build_root, host_target) |
| 52 | + # FIXME: Manually create an empty dir that is usually created during |
| 53 | + # check-symbols. The directory is required during sysroot installation step. |
| 54 | + os.makedirs(os.path.join(sysroot_build_dir, "share"), exist_ok=True) |
| 55 | + |
| 56 | + shell.call([ |
| 57 | + 'make', 'install', |
| 58 | + '-j', str(build_jobs), |
| 59 | + # FIXME: wasi-libc's pre-defined macro list does not expect |
| 60 | + # `__FPCLASS_XXX`, which is introduced by the LLVM 17, yet. |
| 61 | + # So skip the symbol check step by treating the phony target |
| 62 | + # as very old file. |
| 63 | + # https://github.com/llvm/llvm-project/commit/7dd387d2971d7759cadfffeb2082439f6c7ddd49 |
| 64 | + '--old-file=check-symbols', |
| 65 | + '-C', self.source_dir, |
| 66 | + 'OBJDIR=' + os.path.join(self.build_dir, 'obj'), |
| 67 | + 'SYSROOT=' + sysroot_build_dir, |
| 68 | + 'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root), |
| 69 | + 'CC=' + os.path.join(llvm_build_dir, 'bin', 'clang'), |
| 70 | + 'AR=' + os.path.join(llvm_build_dir, 'bin', 'llvm-ar'), |
| 71 | + 'NM=' + os.path.join(llvm_build_dir, 'bin', 'llvm-nm'), |
| 72 | + ]) |
| 73 | + |
| 74 | + @classmethod |
| 75 | + def get_dependencies(cls): |
| 76 | + return [llvm.LLVM] |
| 77 | + |
| 78 | + @classmethod |
| 79 | + def sysroot_build_path(cls, build_root, host_target): |
| 80 | + """ |
| 81 | + Returns the path to the sysroot build directory, which contains only the |
| 82 | + artifacts of wasi-libc (Not including the artifacts of LLVM runtimes). |
| 83 | + """ |
| 84 | + return os.path.join(build_root, |
| 85 | + '%s-%s' % (cls.product_name(), host_target), 'sysroot') |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def sysroot_install_path(cls, build_root): |
| 89 | + """ |
| 90 | + Returns the path to the sysroot install directory, which contains artifacts |
| 91 | + of wasi-libc and LLVM runtimes. |
| 92 | + """ |
| 93 | + return os.path.join(build_root, 'wasi-sysroot') |
| 94 | + |
| 95 | + |
| 96 | +class WasmLLVMRuntimeLibs(cmake_product.CMakeProduct): |
| 97 | + @classmethod |
| 98 | + def product_source_name(cls): |
| 99 | + return os.path.join("llvm-project", "runtimes") |
| 100 | + |
| 101 | + @classmethod |
| 102 | + def is_build_script_impl_product(cls): |
| 103 | + return False |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def is_before_build_script_impl_product(cls): |
| 107 | + return False |
| 108 | + |
| 109 | + def should_build(self, host_target): |
| 110 | + # LLVM runtime libraries should always be built for WebAssembly |
| 111 | + # if standard library is being built for WebAssembly. |
| 112 | + return self.args.build_wasmstdlib |
| 113 | + |
| 114 | + def should_test(self, host_target): |
| 115 | + return False |
| 116 | + |
| 117 | + def should_install(self, host_target): |
| 118 | + return False |
| 119 | + |
| 120 | + def build(self, host_target): |
| 121 | + build_root = os.path.dirname(self.build_dir) |
| 122 | + llvm_build_dir = os.path.join('..', build_root, '%s-%s' % ('llvm', host_target)) |
| 123 | + |
| 124 | + self.cmake_options.define('CMAKE_SYSROOT:PATH', |
| 125 | + WASILibc.sysroot_build_path(build_root, host_target)) |
| 126 | + self.cmake_options.define('LLVM_ENABLE_RUNTIMES:STRING', |
| 127 | + 'libcxx;libcxxabi;compiler-rt') |
| 128 | + self.cmake_options.define('LIBCXX_LIBDIR_SUFFIX:STRING', '/wasm32-wasi') |
| 129 | + self.cmake_options.define('LIBCXXABI_LIBDIR_SUFFIX:STRING', '/wasm32-wasi') |
| 130 | + self.cmake_options.define('CMAKE_STAGING_PREFIX:PATH', '/') |
| 131 | + |
| 132 | + self.cmake_options.define('COMPILER_RT_DEFAULT_TARGET_ARCH:STRING', 'wasm32') |
| 133 | + self.cmake_options.define('COMPILER_RT_DEFAULT_TARGET_ONLY:BOOL', 'TRUE') |
| 134 | + self.cmake_options.define('COMPILER_RT_BAREMETAL_BUILD:BOOL', 'TRUE') |
| 135 | + self.cmake_options.define('COMPILER_RT_BUILD_XRAY:BOOL', 'FALSE') |
| 136 | + self.cmake_options.define('COMPILER_RT_INCLUDE_TESTS:BOOL', 'FALSE') |
| 137 | + self.cmake_options.define('COMPILER_RT_HAS_FPIC_FLAG:BOOL', 'FALSE') |
| 138 | + self.cmake_options.define('COMPILER_RT_EXCLUDE_ATOMIC_BUILTIN:BOOL', 'FALSE') |
| 139 | + self.cmake_options.define('COMPILER_RT_OS_DIR:STRING', 'wasi') |
| 140 | + |
| 141 | + self.cmake_options.define('CMAKE_C_COMPILER_WORKS:BOOL', 'TRUE') |
| 142 | + self.cmake_options.define('CMAKE_CXX_COMPILER_WORKS:BOOL', 'TRUE') |
| 143 | + |
| 144 | + self.cmake_options.define('CMAKE_SYSTEM_NAME:STRING', 'WASI') |
| 145 | + self.cmake_options.define('CMAKE_SYSTEM_PROCESSOR:STRING', 'wasm32') |
| 146 | + self.cmake_options.define('CMAKE_AR:FILEPATH', |
| 147 | + os.path.join(llvm_build_dir, 'bin', 'llvm-ar')) |
| 148 | + self.cmake_options.define('CMAKE_RANLIB:FILEPATH', |
| 149 | + os.path.join(llvm_build_dir, 'bin', 'llvm-ranlib')) |
| 150 | + self.cmake_options.define('CMAKE_C_COMPILER:FILEPATH', |
| 151 | + os.path.join(llvm_build_dir, 'bin', 'clang')) |
| 152 | + self.cmake_options.define('CMAKE_CXX_COMPILER:STRING', |
| 153 | + os.path.join(llvm_build_dir, 'bin', 'clang++')) |
| 154 | + # Explicitly disable exceptions even though it's usually implicitly disabled by |
| 155 | + # LIBCXX_ENABLE_EXCEPTIONS because the CMake feature check fails to detect |
| 156 | + # -fno-exceptions support in clang due to missing compiler-rt while configuring |
| 157 | + # as mono project. |
| 158 | + self.cmake_options.define('CMAKE_CXX_FLAGS:STRING', '-fno-exceptions') |
| 159 | + |
| 160 | + self.cmake_options.define('CMAKE_C_COMPILER_TARGET:STRING', 'wasm32-wasi') |
| 161 | + self.cmake_options.define('CMAKE_CXX_COMPILER_TARGET:STRING', 'wasm32-wasi') |
| 162 | + |
| 163 | + self.cmake_options.define('CXX_SUPPORTS_CXX11:BOOL', 'TRUE') |
| 164 | + |
| 165 | + self.cmake_options.define('LIBCXX_ENABLE_THREADS:BOOL', 'FALSE') |
| 166 | + self.cmake_options.define('LIBCXX_HAS_PTHREAD_API:BOOL', 'FALSE') |
| 167 | + self.cmake_options.define('LIBCXX_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE') |
| 168 | + self.cmake_options.define('LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL', 'FALSE') |
| 169 | + self.cmake_options.define('LIBCXX_HAS_WIN32_THREAD_API:BOOL', 'FALSE') |
| 170 | + self.cmake_options.define('LIBCXX_ENABLE_SHARED:BOOL', 'FALSE') |
| 171 | + self.cmake_options.define('LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL', 'FALSE') |
| 172 | + self.cmake_options.define('LIBCXX_ENABLE_EXCEPTIONS:BOOL', 'FALSE') |
| 173 | + self.cmake_options.define('LIBCXX_ENABLE_FILESYSTEM:BOOL', 'FALSE') |
| 174 | + self.cmake_options.define('LIBCXX_CXX_ABI', 'libcxxabi') |
| 175 | + self.cmake_options.define('LIBCXX_HAS_MUSL_LIBC:BOOL', 'TRUE') |
| 176 | + |
| 177 | + self.cmake_options.define('LIBCXX_ABI_VERSION', '2') |
| 178 | + self.cmake_options.define('LIBCXXABI_ENABLE_EXCEPTIONS:BOOL', 'FALSE') |
| 179 | + self.cmake_options.define('LIBCXXABI_ENABLE_SHARED:BOOL', 'FALSE') |
| 180 | + self.cmake_options.define('LIBCXXABI_SILENT_TERMINATE:BOOL', 'TRUE') |
| 181 | + self.cmake_options.define('LIBCXXABI_ENABLE_THREADS:BOOL', 'FALSE') |
| 182 | + self.cmake_options.define('LIBCXXABI_HAS_PTHREAD_API:BOOL', 'FALSE') |
| 183 | + self.cmake_options.define('LIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL', 'FALSE') |
| 184 | + self.cmake_options.define('LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY:BOOL', |
| 185 | + 'FALSE') |
| 186 | + self.cmake_options.define('LIBCXXABI_HAS_WIN32_THREAD_API:BOOL', 'FALSE') |
| 187 | + self.cmake_options.define('LIBCXXABI_ENABLE_PIC:BOOL', 'FALSE') |
| 188 | + self.cmake_options.define('UNIX:BOOL', 'TRUE') |
| 189 | + |
| 190 | + self.build_with_cmake([], self.args.build_variant, [], |
| 191 | + prefer_just_built_toolchain=True) |
| 192 | + self.install_with_cmake( |
| 193 | + ["install"], WASILibc.sysroot_install_path(build_root)) |
| 194 | + |
| 195 | + @classmethod |
| 196 | + def get_dependencies(cls): |
| 197 | + return [WASILibc, llvm.LLVM] |
0 commit comments