Repository: apple/swift
File: utils/swift_build_support/swift_build_support/products/wasmstdlib.py (Python)

Code before:
        return False

    def should_build(self, host_target):
        return self.args.build_wasmstdlib

    def should_test(self, host_target):
        return self.args.test_wasmstdlib

    def build(self, host_target):
        self._build(host_target, 'wasm32-wasip1', 'wasip1-wasm32')

    def _build(self, host_target, target_triple, short_triple):
        llvm_build_dir = self._configure_llvm(target_triple, short_triple)
        llvm_cmake_dir = os.path.join(llvm_build_dir, 'lib', 'cmake', 'llvm')
        self._build_stdlib(host_target, target_triple, llvm_cmake_dir)

    def _configure_llvm(self, target_triple, short_triple):
        # Configure LLVM for WebAssembly target independently
        # from the native LLVM build to turn off zlib and libxml2
        build_root = os.path.dirname(self.build_dir)
        build_dir = os.path.join(
            build_root, 'llvm-%s' % short_triple)
        llvm_source_dir = os.path.join(
            os.path.dirname(self.source_dir), 'llvm-project', 'llvm')
        cmake_options = cmake.CMakeOptions()
        cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)
        # compiler-rt for WebAssembly target is not installed in the host toolchain
        # so skip compiler health checks here.
        cmake_options.define('CMAKE_C_COMPILER_WORKS:BOOL', 'TRUE')
        cmake_options.define('CMAKE_CXX_COMPILER_WORKS:BOOL', 'TRUE')
        cmake_options.define('LLVM_COMPILER_CHECKED:BOOL', 'TRUE')
        cmake_options.define('LLVM_ENABLE_ZLIB:BOOL', 'FALSE')
        cmake_options.define('LLVM_ENABLE_LIBXML2:BOOL', 'FALSE')
        cmake_options.define('LLVM_ENABLE_LIBEDIT:BOOL', 'FALSE')
        cmake_options.define('LLVM_ENABLE_TERMINFO:BOOL', 'FALSE')
        if self.args.build_runtime_with_host_compiler:
            cmake_options.define('CMAKE_ASM_COMPILER:PATH', self.toolchain.cc)

        llvm_cmake = cmake.CMake(
            self.args, self.toolchain, prefer_native_toolchain=not self.args.build_runtime_with_host_compiler)
        # Only configure LLVM, not build it because we just need
        # LLVM CMake functionalities
        shell.call(["env", self.toolchain.cmake, "-B", build_dir]
                   + list(llvm_cmake.common_options(self))
                   + list(cmake_options)
                   + [llvm_source_dir])
        return build_dir

    def _build_stdlib(self, host_target, target_triple, llvm_cmake_dir):
        self.cmake_options.define('CMAKE_INSTALL_PREFIX:PATH', '/usr')
        self.cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)

Review comment (suggestion):
Would you mind elaborating on why we need to include CMake options for host platform (like `CMAKE_OSX_SYSROOT`) here? Mixing host and target options is something I'd like to avoid in general.

Code after:
        return False

    def should_build(self, host_target):
        return self.args.build_wasmstdlib

    def should_test(self, host_target):
        return self.args.test_wasmstdlib

    def build(self, host_target):
        self._build(host_target, 'wasm32-wasip1', 'wasip1-wasm32')

    def _build(self, host_target, target_triple, short_triple):
        llvm_build_dir = self._configure_llvm(host_target, target_triple, short_triple)
        llvm_cmake_dir = os.path.join(llvm_build_dir, 'lib', 'cmake', 'llvm')
        self._build_stdlib(host_target, target_triple, llvm_cmake_dir)

    def _configure_llvm(self, host_target, target_triple, short_triple):
        # Configure LLVM for WebAssembly target independently
        # from the native LLVM build to turn off zlib and libxml2
        build_root = os.path.dirname(self.build_dir)
        build_dir = os.path.join(
            build_root, 'llvm-%s' % short_triple)
        llvm_source_dir = os.path.join(
            os.path.dirname(self.source_dir), 'llvm-project', 'llvm')

        cmake_options, _, relevant_options = self.host_cmake_options(host_target)
        cmake_options.define('CMAKE_BUILD_TYPE:STRING', self._build_variant)
        # compiler-rt for WebAssembly target is not installed in the host toolchain
        # so skip compiler health checks here.
        cmake_options.define('CMAKE_C_COMPILER_WORKS:BOOL', 'TRUE')
        cmake_options.define('CMAKE_CXX_COMPILER_WORKS:BOOL', 'TRUE')
        cmake_options.define('LLVM_COMPILER_CHECKED:BOOL', 'TRUE')
        cmake_options.define('LLVM_ENABLE_ZLIB:BOOL', 'FALSE')
        cmake_options.define('LLVM_ENABLE_LIBXML2:BOOL', 'FALSE')
        cmake_options.define('LLVM_ENABLE_LIBEDIT:BOOL', 'FALSE')
        cmake_options.define('LLVM_ENABLE_TERMINFO:BOOL', 'FALSE')
        if self.args.build_runtime_with_host_compiler:
            cmake_options.define('CMAKE_ASM_COMPILER:PATH', self.toolchain.cc)

        llvm_cmake = cmake.CMake(
            self.args, self.toolchain, prefer_native_toolchain=not self.args.build_runtime_with_host_compiler)
        # Only configure LLVM, not build it because we just need
        # LLVM CMake functionalities
        shell.call(["env", self.toolchain.cmake, "-B", build_dir]
                   + list(llvm_cmake.common_options(self))
                   + list(cmake_options)
                   + [llvm_source_dir])
        return build_dir

    def _build_stdlib(self, host_target, target_triple, llvm_cmake_dir):
        self.cmake_options.define('CMAKE_INSTALL_PREFIX:PATH', '/usr')