diff --git a/tools/ci_build/build.py b/tools/ci_build/build.py index 561a76be5fa89..56fd3f1323e92 100644 --- a/tools/ci_build/build.py +++ b/tools/ci_build/build.py @@ -620,6 +620,7 @@ def generate_build_tree( ) generate_vcpkg_triplets_for_emscripten( build_dir, + configs, emscripten_root_path, not args.disable_rtti, not args.disable_wasm_exception_catching, @@ -627,25 +628,21 @@ def generate_build_tree( args.enable_address_sanitizer, ) elif args.android: - generate_android_triplets(build_dir, args.android_cpp_shared, args.android_api) + generate_android_triplets(build_dir, configs, args.android_cpp_shared, args.android_api) elif is_windows(): - generate_windows_triplets(build_dir, args.msvc_toolset) + generate_windows_triplets(build_dir, configs, args.msvc_toolset) elif is_macOS(): osx_target = args.apple_deploy_target if args.apple_deploy_target is None: osx_target = os.environ.get("MACOSX_DEPLOYMENT_TARGET") if osx_target is not None: log.info(f"Setting VCPKG_OSX_DEPLOYMENT_TARGET to {osx_target}") - generate_macos_triplets(build_dir, osx_target) + generate_macos_triplets(build_dir, configs, osx_target) else: # Linux, *BSD, AIX or other platforms - generate_linux_triplets(build_dir) + generate_linux_triplets(build_dir, configs) add_default_definition(cmake_extra_defines, "CMAKE_TOOLCHAIN_FILE", str(vcpkg_toolchain_path)) - vcpkg_install_options = generate_vcpkg_install_options(build_dir, args) - # VCPKG_INSTALL_OPTIONS is a CMake list. It must be joined by semicolons - # Therefore, if any of the option string contains a semicolon, it must be escaped - add_default_definition(cmake_extra_defines, "VCPKG_INSTALL_OPTIONS", ";".join(vcpkg_install_options)) # Choose the cmake triplet triplet = None if args.build_wasm: @@ -1251,6 +1248,16 @@ def generate_build_tree( ] env = {} if args.use_vcpkg: + # append VCPKG_INSTALL_OPTIONS + # + # VCPKG_INSTALL_OPTIONS is a CMake list. It must be joined by semicolons + # Therefore, if any of the option string contains a semicolon, it must be escaped + temp_cmake_args += [ + "-DVCPKG_INSTALL_OPTIONS={}".format( + ";".join(generate_vcpkg_install_options(Path(build_dir) / config, args)) + ) + ] + vcpkg_keep_env_vars = ["TRT_UPLOAD_AUTH_TOKEN"] if args.build_wasm: diff --git a/tools/ci_build/github/azure-pipelines/linux-gpu-tensorrt-cuda-minimal-ci-pipeline.yml b/tools/ci_build/github/azure-pipelines/linux-gpu-tensorrt-cuda-minimal-ci-pipeline.yml index b304ccdb4c533..0410001d77d13 100644 --- a/tools/ci_build/github/azure-pipelines/linux-gpu-tensorrt-cuda-minimal-ci-pipeline.yml +++ b/tools/ci_build/github/azure-pipelines/linux-gpu-tensorrt-cuda-minimal-ci-pipeline.yml @@ -65,6 +65,12 @@ jobs: clean: true submodules: none + - task: UsePythonVersion@0 + inputs: + versionSpec: '3.12' + addToPath: true + architecture: 'x64' + - template: templates/get-docker-image-steps.yml parameters: Dockerfile: tools/ci_build/github/linux/docker/Dockerfile.manylinux2_28_cuda diff --git a/tools/python/util/vcpkg_helpers.py b/tools/python/util/vcpkg_helpers.py index 1fbb4a06b3c2b..a9f1420946354 100644 --- a/tools/python/util/vcpkg_helpers.py +++ b/tools/python/util/vcpkg_helpers.py @@ -93,8 +93,29 @@ def add_copyright_header(f) -> None: ) +def add_build_type(f, build_type: str) -> None: + """ + Add build type to the triplet file. + + Args: + f (file object): The file object to write the build type. + build_type (str): The build type to add. Must be one of "Debug", "Release", "RelWithDebInfo", or "MinSizeRel". + """ + if build_type not in ["Debug", "Release", "RelWithDebInfo", "MinSizeRel"]: + raise ValueError( + f"Invalid build type: {build_type}. Must be one of 'Debug', 'Release', 'RelWithDebInfo', or 'MinSizeRel'." + ) + + if build_type != "Debug": + f.write( + """set(VCPKG_BUILD_TYPE release) +""" + ) + + def generate_triplet_for_android( build_dir: str, + configs: set[str], target_abi: str, enable_rtti: bool, enable_exception: bool, @@ -130,100 +151,102 @@ def generate_triplet_for_android( file_name = f"{target_abi}-android.cmake" - dest_path = Path(build_dir) / folder_name / file_name + for config in configs: + dest_path = Path(build_dir) / config / folder_name / file_name + + os.makedirs(dest_path.parent, exist_ok=True) - os.makedirs(dest_path.parent, exist_ok=True) + with open(dest_path, "w", encoding="utf-8") as f: + add_copyright_header(f) - with open(dest_path, "w", encoding="utf-8") as f: - add_copyright_header(f) + # Set target architecture for Android + if target_abi == "arm-neon": + f.write("set(VCPKG_TARGET_ARCHITECTURE arm)\n") + f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=armv7a-linux-androideabi)\n") + f.write( + "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=armeabi-v7a -DANDROID_ARM_NEON=ON -DCMAKE_ANDROID_ARM_NEON=ON)\n" + ) + elif target_abi == "arm64": + f.write("set(VCPKG_TARGET_ARCHITECTURE arm64)\n") + f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=aarch64-linux-android)\n") + f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=arm64-v8a)\n") + elif target_abi == "x64": + f.write("set(VCPKG_TARGET_ARCHITECTURE x64)\n") + f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=x86_64-linux-android)\n") + f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=x86_64)\n") + elif target_abi == "x86": + f.write("set(VCPKG_TARGET_ARCHITECTURE x86)\n") + f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=i686-linux-android)\n") + f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=x86)\n") - # Set target architecture for Android - if target_abi == "arm-neon": - f.write("set(VCPKG_TARGET_ARCHITECTURE arm)\n") - f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=armv7a-linux-androideabi)\n") f.write( - "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=armeabi-v7a -DANDROID_ARM_NEON=ON -DCMAKE_ANDROID_ARM_NEON=ON)\n" + f"list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_USE_LEGACY_TOOLCHAIN_FILE=false -DANDROID_PLATFORM=android-{android_api_level} -DANDROID_MIN_SDK={android_api_level})\n" ) - elif target_abi == "arm64": - f.write("set(VCPKG_TARGET_ARCHITECTURE arm64)\n") - f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=aarch64-linux-android)\n") - f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=arm64-v8a)\n") - elif target_abi == "x64": - f.write("set(VCPKG_TARGET_ARCHITECTURE x64)\n") - f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=x86_64-linux-android)\n") - f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=x86_64)\n") - elif target_abi == "x86": - f.write("set(VCPKG_TARGET_ARCHITECTURE x86)\n") - f.write("set(VCPKG_MAKE_BUILD_TRIPLET --host=i686-linux-android)\n") - f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_ABI=x86)\n") - f.write( - f"list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DANDROID_USE_LEGACY_TOOLCHAIN_FILE=false -DANDROID_PLATFORM=android-{android_api_level} -DANDROID_MIN_SDK={android_api_level})\n" - ) - - # Set CRT linkage - # VCPKG_CRT_LINKAGE specifies the desired CRT linkage (for MSVC). - # Valid options are dynamic and static. - crt_linkage = "static" - f.write(f"set(VCPKG_CRT_LINKAGE {crt_linkage})\n") + # Set CRT linkage + # VCPKG_CRT_LINKAGE specifies the desired CRT linkage (for MSVC). + # Valid options are dynamic and static. + crt_linkage = "static" + f.write(f"set(VCPKG_CRT_LINKAGE {crt_linkage})\n") - # Set library linkage - # VCPKG_LIBRARY_LINKAGE specifies the preferred library linkage. - # Valid options are dynamic and static. Libraries can ignore this setting if they do not support the preferred linkage type. In our case, we prefer to use static libs. - f.write("set(VCPKG_LIBRARY_LINKAGE static)\n") - if not enable_rtti: - f.write("set(CMAKE_ANDROID_RTTI OFF)\n") - if not enable_exception: - f.write("set(CMAKE_ANDROID_EXCEPTIONS OFF)\n") - if use_cpp_shared: - f.write("set(ANDROID_STL c++_shared)\n") + # Set library linkage + # VCPKG_LIBRARY_LINKAGE specifies the preferred library linkage. + # Valid options are dynamic and static. Libraries can ignore this setting if they do not support the preferred linkage type. In our case, we prefer to use static libs. + f.write("set(VCPKG_LIBRARY_LINKAGE static)\n") + if not enable_rtti: + f.write("set(CMAKE_ANDROID_RTTI OFF)\n") + if not enable_exception: + f.write("set(CMAKE_ANDROID_EXCEPTIONS OFF)\n") + if use_cpp_shared: + f.write("set(ANDROID_STL c++_shared)\n") - ldflags = [] + ldflags = [] - cflags = ["-g", "-ffunction-sections", "-fdata-sections"] - cflags_release = ["-DNDEBUG", "-O3"] + cflags = ["-g", "-ffunction-sections", "-fdata-sections"] + cflags_release = ["-DNDEBUG", "-O3"] - if enable_asan: - cflags += ["-fsanitize=address"] - ldflags += ["-fsanitize=address"] + if enable_asan: + cflags += ["-fsanitize=address"] + ldflags += ["-fsanitize=address"] - ldflags.append("-g") + ldflags.append("-g") - cxxflags = cflags.copy() + cxxflags = cflags.copy() - if not enable_rtti: - cxxflags.append("-fno-rtti") + if not enable_rtti: + cxxflags.append("-fno-rtti") - if not enable_exception: - cxxflags += ["-fno-exceptions", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables"] + if not enable_exception: + cxxflags += ["-fno-exceptions", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables"] - if cflags: - f.write(f'set(VCPKG_C_FLAGS "{" ".join(cflags)}")\n') + if cflags: + f.write(f'set(VCPKG_C_FLAGS "{" ".join(cflags)}")\n') - if cxxflags: - f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') + if cxxflags: + f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') - if cflags_release: - f.write(f'set(VCPKG_C_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') - f.write(f'set(VCPKG_CXX_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') - f.write(f'set(VCPKG_C_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') - f.write(f'set(VCPKG_CXX_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') + if cflags_release: + f.write(f'set(VCPKG_C_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') + f.write(f'set(VCPKG_CXX_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') + f.write(f'set(VCPKG_C_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') + f.write(f'set(VCPKG_CXX_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') - # Set target platform - # VCPKG_CMAKE_SYSTEM_NAME specifies the target platform. - f.write("set(VCPKG_CMAKE_SYSTEM_NAME Android)\n") - f.write("set(CMAKE_POSITION_INDEPENDENT_CODE ON)\n") - f.write( - "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS --compile-no-warning-as-error -DBENCHMARK_ENABLE_WERROR=OFF)\n" - ) + # Set target platform + # VCPKG_CMAKE_SYSTEM_NAME specifies the target platform. + f.write("set(VCPKG_CMAKE_SYSTEM_NAME Android)\n") + f.write("set(CMAKE_POSITION_INDEPENDENT_CODE ON)\n") + f.write( + "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS --compile-no-warning-as-error -DBENCHMARK_ENABLE_WERROR=OFF)\n" + ) - if ldflags: - f.write(f'set(VCPKG_LINKER_FLAGS "{" ".join(ldflags)}")\n') - f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DCMAKE_CXX_STANDARD=17)\n") - add_port_configs(f, enable_exception, False, enable_minimal_build) # Pass enable_minimal_build + if ldflags: + f.write(f'set(VCPKG_LINKER_FLAGS "{" ".join(ldflags)}")\n') + f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DCMAKE_CXX_STANDARD=17)\n") + add_build_type(f, config) + add_port_configs(f, enable_exception, False, enable_minimal_build) # Pass enable_minimal_build -def generate_android_triplets(build_dir: str, use_cpp_shared: bool, android_api_level: int) -> None: +def generate_android_triplets(build_dir: str, configs: set[str], use_cpp_shared: bool, android_api_level: int) -> None: """ Generate triplet files for POSIX platforms (Linux, macOS, Android). @@ -240,6 +263,7 @@ def generate_android_triplets(build_dir: str, use_cpp_shared: bool, android_api_ for target_abi in target_abis: generate_triplet_for_android( build_dir, + configs, target_abi, enable_rtti, enable_exception, @@ -252,6 +276,7 @@ def generate_android_triplets(build_dir: str, use_cpp_shared: bool, android_api_ def generate_triplet_for_posix_platform( build_dir: str, + configs: set[str], os_name: str, enable_rtti: bool, enable_exception: bool, @@ -293,115 +318,118 @@ def generate_triplet_for_posix_platform( file_name = f"{target_abi}-{os_name}.cmake" - dest_path = Path(build_dir) / folder_name / file_name - - os.makedirs(dest_path.parent, exist_ok=True) - - with open(dest_path, "w", encoding="utf-8") as f: - add_copyright_header(f) - - # Set target architecture based on `os_name` and `target_abi`. - # - # In most cases VCPKG itself can help automatically detect the target architecture, but sometimes it is not as what we want. The following code process the special cases. - if target_abi == "universal2": - # Assume the host machine is Intel based - f.write("set(VCPKG_TARGET_ARCHITECTURE x64)\n") - else: - f.write(f"set(VCPKG_TARGET_ARCHITECTURE {target_abi})\n") - - # Set CRT linkage - # VCPKG_CRT_LINKAGE specifies the desired CRT linkage (for MSVC). - # Valid options are dynamic and static. - f.write(f"set(VCPKG_CRT_LINKAGE {crt_linkage})\n") - - # Set library linkage - # VCPKG_LIBRARY_LINKAGE specifies the preferred library linkage. - # Valid options are dynamic and static. Libraries can ignore this setting if they do not support the preferred linkage type. - f.write("set(VCPKG_LIBRARY_LINKAGE static)\n") - - ldflags = [] - - if enable_binskim and os_name == "linux": - # BinSkim rule 3005: Enable stack clash protection - # This check ensures that stack clash protection is enabled. Each program running on a computer uses a special memory region called the stack. - # This memory region is special because it grows automatically when the program needs more stack memory. But if it grows too much and gets too close to another memory region, - # the program may confuse the stack with the other memory region. An attacker can exploit this confusion to overwrite the stack with the other memory region, or the other way around. - # Use the compiler flags '-fstack-clash-protection' to enable this. - # BinSkim rule BA3011: Enable BIND_NOW - # This check ensures that some relocation data is marked as read-only after the executable is loaded, and moved below the '.data' section in memory. - # This prevents them from being overwritten, which can redirect control flow. Use the compiler flags '-Wl,-z,now' to enable this. - ldflags = ["-Wl,-Bsymbolic-functions", "-Wl,-z,relro", "-Wl,-z,now", "-Wl,-z,noexecstack"] - - cflags = ["-g", "-ffunction-sections", "-fdata-sections"] - cflags_release = ["-DNDEBUG", "-O3"] - - if enable_binskim: - cflags_release += ["-Wp,-D_FORTIFY_SOURCE=2", "-Wp,-D_GLIBCXX_ASSERTIONS", "-fstack-protector-strong"] - if target_abi == "x64": - cflags_release += ["-fstack-clash-protection", "-fcf-protection"] - - elif enable_asan: - cflags += ["-fsanitize=address"] - ldflags += ["-fsanitize=address"] - - ldflags.append("-g") - - if not enable_rtti: - cflags.append("-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0") - - cxxflags = cflags.copy() - if os_name == "osx": - cxxflags += ["-fvisibility=hidden", "-fvisibility-inlines-hidden"] - if not enable_rtti: - cxxflags.append("-fno-rtti") - - if not enable_exception: - cxxflags += ["-fno-exceptions", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables"] - - if cflags: - f.write(f'set(VCPKG_C_FLAGS "{" ".join(cflags)}")\n') - - if cxxflags: - f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') - - if cflags_release: - f.write(f'set(VCPKG_C_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') - f.write(f'set(VCPKG_CXX_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') - f.write(f'set(VCPKG_C_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') - f.write(f'set(VCPKG_CXX_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') - - # Set target platform - # VCPKG_CMAKE_SYSTEM_NAME specifies the target platform. - if os_name == "linux": - f.write("set(VCPKG_CMAKE_SYSTEM_NAME Linux)\n") - else: - f.write("set(VCPKG_CMAKE_SYSTEM_NAME Darwin)\n") - osx_abi = None - if target_abi == "x64": - osx_abi = "x86_64" - elif target_abi == "universal2": - osx_abi = "x86_64;arm64" + for config in configs: + dest_path = Path(build_dir) / config / folder_name / file_name + + os.makedirs(dest_path.parent, exist_ok=True) + + with open(dest_path, "w", encoding="utf-8") as f: + add_copyright_header(f) + + # Set target architecture based on `os_name` and `target_abi`. + # + # In most cases VCPKG itself can help automatically detect the target architecture, but sometimes it is not as what we want. The following code process the special cases. + if target_abi == "universal2": + # Assume the host machine is Intel based + f.write("set(VCPKG_TARGET_ARCHITECTURE x64)\n") else: - osx_abi = target_abi - f.write(f'set(VCPKG_OSX_ARCHITECTURES "{osx_abi}")\n') - if osx_deployment_target: - f.write(f'set(VCPKG_OSX_DEPLOYMENT_TARGET "{osx_deployment_target}")\n') - f.write("set(CMAKE_POSITION_INDEPENDENT_CODE ON)\n") - f.write( - "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS --compile-no-warning-as-error -DBENCHMARK_ENABLE_WERROR=OFF)\n" - ) + f.write(f"set(VCPKG_TARGET_ARCHITECTURE {target_abi})\n") - if ldflags: - f.write(f'set(VCPKG_LINKER_FLAGS "{" ".join(ldflags)}")\n') - if os_name == "osx": - f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DCMAKE_CXX_STANDARD=20)\n") - else: - f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DCMAKE_CXX_STANDARD=17)\n") - add_port_configs(f, enable_exception, False, enable_minimal_build) # Pass enable_minimal_build + # Set CRT linkage + # VCPKG_CRT_LINKAGE specifies the desired CRT linkage (for MSVC). + # Valid options are dynamic and static. + f.write(f"set(VCPKG_CRT_LINKAGE {crt_linkage})\n") + + # Set library linkage + # VCPKG_LIBRARY_LINKAGE specifies the preferred library linkage. + # Valid options are dynamic and static. Libraries can ignore this setting if they do not support the preferred linkage type. + f.write("set(VCPKG_LIBRARY_LINKAGE static)\n") + + ldflags = [] + + if enable_binskim and os_name == "linux": + # BinSkim rule 3005: Enable stack clash protection + # This check ensures that stack clash protection is enabled. Each program running on a computer uses a special memory region called the stack. + # This memory region is special because it grows automatically when the program needs more stack memory. But if it grows too much and gets too close to another memory region, + # the program may confuse the stack with the other memory region. An attacker can exploit this confusion to overwrite the stack with the other memory region, or the other way around. + # Use the compiler flags '-fstack-clash-protection' to enable this. + # BinSkim rule BA3011: Enable BIND_NOW + # This check ensures that some relocation data is marked as read-only after the executable is loaded, and moved below the '.data' section in memory. + # This prevents them from being overwritten, which can redirect control flow. Use the compiler flags '-Wl,-z,now' to enable this. + ldflags = ["-Wl,-Bsymbolic-functions", "-Wl,-z,relro", "-Wl,-z,now", "-Wl,-z,noexecstack"] + + cflags = ["-g", "-ffunction-sections", "-fdata-sections"] + cflags_release = ["-DNDEBUG", "-O3"] + + if enable_binskim: + cflags_release += ["-Wp,-D_FORTIFY_SOURCE=2", "-Wp,-D_GLIBCXX_ASSERTIONS", "-fstack-protector-strong"] + if target_abi == "x64": + cflags_release += ["-fstack-clash-protection", "-fcf-protection"] + + elif enable_asan: + cflags += ["-fsanitize=address"] + ldflags += ["-fsanitize=address"] + + ldflags.append("-g") + + if not enable_rtti: + cflags.append("-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0") + + cxxflags = cflags.copy() + if os_name == "osx": + cxxflags += ["-fvisibility=hidden", "-fvisibility-inlines-hidden"] + if not enable_rtti: + cxxflags.append("-fno-rtti") + + if not enable_exception: + cxxflags += ["-fno-exceptions", "-fno-unwind-tables", "-fno-asynchronous-unwind-tables"] + + if cflags: + f.write(f'set(VCPKG_C_FLAGS "{" ".join(cflags)}")\n') + + if cxxflags: + f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') + + if cflags_release: + f.write(f'set(VCPKG_C_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') + f.write(f'set(VCPKG_CXX_FLAGS_RELEASE "{" ".join(cflags_release)}")\n') + f.write(f'set(VCPKG_C_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') + f.write(f'set(VCPKG_CXX_FLAGS_RELWITHDEBINFO "{" ".join(cflags_release)}")\n') + + # Set target platform + # VCPKG_CMAKE_SYSTEM_NAME specifies the target platform. + if os_name == "linux": + f.write("set(VCPKG_CMAKE_SYSTEM_NAME Linux)\n") + else: + f.write("set(VCPKG_CMAKE_SYSTEM_NAME Darwin)\n") + osx_abi = None + if target_abi == "x64": + osx_abi = "x86_64" + elif target_abi == "universal2": + osx_abi = "x86_64;arm64" + else: + osx_abi = target_abi + f.write(f'set(VCPKG_OSX_ARCHITECTURES "{osx_abi}")\n') + if osx_deployment_target: + f.write(f'set(VCPKG_OSX_DEPLOYMENT_TARGET "{osx_deployment_target}")\n') + f.write("set(CMAKE_POSITION_INDEPENDENT_CODE ON)\n") + f.write( + "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS --compile-no-warning-as-error -DBENCHMARK_ENABLE_WERROR=OFF)\n" + ) + + if ldflags: + f.write(f'set(VCPKG_LINKER_FLAGS "{" ".join(ldflags)}")\n') + if os_name == "osx": + f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DCMAKE_CXX_STANDARD=20)\n") + else: + f.write("list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS -DCMAKE_CXX_STANDARD=17)\n") + add_build_type(f, config) + add_port_configs(f, enable_exception, False, enable_minimal_build) # Pass enable_minimal_build def generate_vcpkg_triplets_for_emscripten( build_dir: str, + configs: set[str], emscripten_root: str, # Parameters defining the specific build configuration enable_rtti: bool, @@ -449,105 +477,108 @@ def generate_vcpkg_triplets_for_emscripten( for target_abi in ["wasm32", "wasm64"]: os_name = "emscripten" file_name = f"{target_abi}-{os_name}.cmake" - dest_path = Path(build_dir) / folder_name / file_name - os.makedirs(dest_path.parent, exist_ok=True) + for config in configs: + dest_path = Path(build_dir) / config / folder_name / file_name + os.makedirs(dest_path.parent, exist_ok=True) - with open(dest_path, "w", encoding="utf-8") as f: - add_copyright_header(f) - f.write(r""" + with open(dest_path, "w", encoding="utf-8") as f: + add_copyright_header(f) + f.write(r""" set(VCPKG_CRT_LINKAGE dynamic) set(VCPKG_LIBRARY_LINKAGE static) set(VCPKG_CMAKE_SYSTEM_NAME Emscripten) """) - f.write(f"set(VCPKG_TARGET_ARCHITECTURE {target_abi})\n") - emscripten_root_path_cmake_path = emscripten_root.replace("\\", "/") - f.write(f'set(EMSCRIPTEN_ROOT_PATH "{emscripten_root_path_cmake_path}")\n') - - # Define the path to the intermediate toolchain file used by vcpkg for wasm - vcpkg_toolchain_file = (Path(build_dir) / "emsdk_vcpkg_toolchain.cmake").absolute() - vcpkg_toolchain_file_cmake_path = str(vcpkg_toolchain_file).replace("\\", "/") - f.write(f'set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "{vcpkg_toolchain_file_cmake_path}")\n') - - # --- Configure Flags based on Parameters --- - cflags_release = ["-DNDEBUG", "-O3", "-flto"] - ldflags = [] # Initialize linker flags list - # Base flags applicable to both C and C++ - base_flags = [ - "-ffunction-sections", - "-fdata-sections", - "-msimd128", - "-pthread", - "-Wno-pthreads-mem-growth", - ] - - # ASan (apply to Base, Linker) - if enable_asan: - asan_flag = "-fsanitize=address" - base_flags.append(asan_flag) - ldflags.append(asan_flag) # Add to linker flags - - # Wasm Exception Catching Runtime (-s flag, apply to Base and Linker flags) - exception_catching_flag = "" - if enable_wasm_exception_catching: - exception_catching_flag = "-sDISABLE_EXCEPTION_CATCHING=0" - else: - exception_catching_flag = "-sDISABLE_EXCEPTION_CATCHING=1" - - base_flags.append(exception_catching_flag) # Add to base C/C++ flags - ldflags.append(exception_catching_flag) # Add to linker flags - - # Wasm64 Memory (apply to Base, Linker) - if target_abi == "wasm64": - memory_flag = "-sMEMORY64" - base_flags.append(memory_flag) - ldflags.append(memory_flag) # Add to linker flags - - # --- C Flags --- - # VCPKG_C_FLAGS applies only base flags - f.write(f'set(VCPKG_C_FLAGS "{" ".join(base_flags)}")\n') - - # --- CXX Flags --- - # Start with base flags - cxxflags = list(base_flags) # Create a copy - - # C++ RTTI Compiler Flag - if not enable_rtti: - cxxflags.append("-fno-rtti") - - # C++ Exceptions Compiler Flag (Derived from enable_minimal_onnx_build) - if not cpp_exceptions_enabled: # i.e., if enable_minimal_onnx_build is True - cxxflags.append("-fno-exceptions") - # If cpp_exceptions_enabled=True, we assume -fexceptions is the default - # or handled by the Emscripten toolchain/CMake settings elsewhere. - - f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') - - # --- Linker Flags --- - # Apply Linker flags (now includes exception and memory flags explicitly) - if len(ldflags) >= 1: - f.write('set(VCPKG_LINKER_FLAGS "{}")\n'.format(" ".join(ldflags))) - - # --- Release / RelWithDebInfo Flags --- - # Combine base flags with release-specific flags - c_combined_release_flags = cflags_release + base_flags - cxx_combined_release_flags = cflags_release + cxxflags # Use the derived cxxflags - - f.write(f'set(VCPKG_C_FLAGS_RELEASE "{" ".join(c_combined_release_flags)}")\n') - f.write(f'set(VCPKG_CXX_FLAGS_RELEASE "{" ".join(cxx_combined_release_flags)}")\n') - - f.write("set(VCPKG_LINKER_FLAGS_RELEASE -flto)\n") - - # --- Add Port Specific Configs --- - # Pass the derived C++ exception status and the original minimal build flag - add_port_configs( - f, - has_exception=cpp_exceptions_enabled, # Derived value - is_emscripten=True, - enable_minimal_build=enable_minimal_onnx_build, - ) # Original parameter - - -def generate_windows_triplets(build_dir: str, toolset_version: str) -> None: + f.write(f"set(VCPKG_TARGET_ARCHITECTURE {target_abi})\n") + emscripten_root_path_cmake_path = emscripten_root.replace("\\", "/") + f.write(f'set(EMSCRIPTEN_ROOT_PATH "{emscripten_root_path_cmake_path}")\n') + + # Define the path to the intermediate toolchain file used by vcpkg for wasm + vcpkg_toolchain_file = (Path(build_dir) / "emsdk_vcpkg_toolchain.cmake").absolute() + vcpkg_toolchain_file_cmake_path = str(vcpkg_toolchain_file).replace("\\", "/") + f.write(f'set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "{vcpkg_toolchain_file_cmake_path}")\n') + + # --- Configure Flags based on Parameters --- + cflags_release = ["-DNDEBUG", "-O3", "-flto"] + ldflags = [] # Initialize linker flags list + # Base flags applicable to both C and C++ + base_flags = [ + "-ffunction-sections", + "-fdata-sections", + "-msimd128", + "-pthread", + "-Wno-pthreads-mem-growth", + ] + + # ASan (apply to Base, Linker) + if enable_asan: + asan_flag = "-fsanitize=address" + base_flags.append(asan_flag) + ldflags.append(asan_flag) # Add to linker flags + + # Wasm Exception Catching Runtime (-s flag, apply to Base and Linker flags) + exception_catching_flag = "" + if enable_wasm_exception_catching: + exception_catching_flag = "-sDISABLE_EXCEPTION_CATCHING=0" + else: + exception_catching_flag = "-sDISABLE_EXCEPTION_CATCHING=1" + + base_flags.append(exception_catching_flag) # Add to base C/C++ flags + ldflags.append(exception_catching_flag) # Add to linker flags + + # Wasm64 Memory (apply to Base, Linker) + if target_abi == "wasm64": + memory_flag = "-sMEMORY64" + base_flags.append(memory_flag) + ldflags.append(memory_flag) # Add to linker flags + + # --- C Flags --- + # VCPKG_C_FLAGS applies only base flags + f.write(f'set(VCPKG_C_FLAGS "{" ".join(base_flags)}")\n') + + # --- CXX Flags --- + # Start with base flags + cxxflags = list(base_flags) # Create a copy + + # C++ RTTI Compiler Flag + if not enable_rtti: + cxxflags.append("-fno-rtti") + + # C++ Exceptions Compiler Flag (Derived from enable_minimal_onnx_build) + if not cpp_exceptions_enabled: # i.e., if enable_minimal_onnx_build is True + cxxflags.append("-fno-exceptions") + # If cpp_exceptions_enabled=True, we assume -fexceptions is the default + # or handled by the Emscripten toolchain/CMake settings elsewhere. + + f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') + + # --- Linker Flags --- + # Apply Linker flags (now includes exception and memory flags explicitly) + if len(ldflags) >= 1: + f.write('set(VCPKG_LINKER_FLAGS "{}")\n'.format(" ".join(ldflags))) + + # --- Release / RelWithDebInfo Flags --- + # Combine base flags with release-specific flags + c_combined_release_flags = cflags_release + base_flags + cxx_combined_release_flags = cflags_release + cxxflags # Use the derived cxxflags + + f.write(f'set(VCPKG_C_FLAGS_RELEASE "{" ".join(c_combined_release_flags)}")\n') + f.write(f'set(VCPKG_CXX_FLAGS_RELEASE "{" ".join(cxx_combined_release_flags)}")\n') + + f.write("set(VCPKG_LINKER_FLAGS_RELEASE -flto)\n") + + add_build_type(f, config) + + # --- Add Port Specific Configs --- + # Pass the derived C++ exception status and the original minimal build flag + add_port_configs( + f, + has_exception=cpp_exceptions_enabled, # Derived value + is_emscripten=True, + enable_minimal_build=enable_minimal_onnx_build, + ) # Original parameter + + +def generate_windows_triplets(build_dir: str, configs: set[str], toolset_version: str) -> None: """ Generate triplet files for Windows platforms. @@ -593,54 +624,56 @@ def generate_windows_triplets(build_dir: str, toolset_version: str) -> None: if crt_linkage == "dynamic": file_name_parts.append("md") file_name = "-".join(file_name_parts) + ".cmake" - dest_path = Path(build_dir) / folder_name / file_name - os.makedirs(dest_path.parent, exist_ok=True) - with open(dest_path, "w", encoding="utf-8") as f: - add_copyright_header(f) - f.write(f"set(VCPKG_TARGET_ARCHITECTURE {target_abi})\n") - f.write(f"set(VCPKG_CRT_LINKAGE {crt_linkage})\n") - f.write("set(VCPKG_LIBRARY_LINKAGE static)\n") - if toolset_version: - f.write(f"set(VCPKG_PLATFORM_TOOLSET_VERSION {toolset_version})\n") - cflags = ["/MP", "/DWIN32", "/D_WINDOWS"] - if enable_binskim: - cflags += [ - "/DWINAPI_FAMILY=100", - "/DWINVER=0x0A00", - "/D_WIN32_WINNT=0x0A00", - "/DNTDDI_VERSION=0x0A000000", - ] - ldflags = [] - if enable_binskim: - cflags += ["/guard:cf", "/Qspectre", "/W3"] - ldflags = ["/profile", "/DYNAMICBASE"] - elif enable_asan: - cflags.append("/fsanitize=address") - cxxflags = cflags.copy() - cxxflags.append("/Zc:__cplusplus") - if enable_exception: - cxxflags.append("/EHsc") - # MSVC doesn't have a specific flag to disable exceptions like /EHs-c- - # but relies on _HAS_EXCEPTIONS=0 and potentially other flags managed by ORT's main CMake. - # Vcpkg doesn't directly control this via a simple triplet flag AFAIK. - # ORT's CMake handles this via CMAKE_CXX_FLAGS adjustment. - if not enable_rtti: - cxxflags += ["/GR-", "/we4541"] - if cflags: - f.write(f'set(VCPKG_C_FLAGS "{" ".join(cflags)}")\n') - if cxxflags: - f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') - f.write( - "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS --compile-no-warning-as-error -DCMAKE_CXX_STANDARD=17)\n" - ) - if ldflags: - f.write(f'set(VCPKG_LINKER_FLAGS "{" ".join(ldflags)}")\n') - add_port_configs( - f, enable_exception, False, enable_minimal_build - ) # Pass enable_minimal_build - - -def generate_linux_triplets(build_dir: str) -> None: + for config in configs: + dest_path = Path(build_dir) / config / folder_name / file_name + os.makedirs(dest_path.parent, exist_ok=True) + with open(dest_path, "w", encoding="utf-8") as f: + add_copyright_header(f) + f.write(f"set(VCPKG_TARGET_ARCHITECTURE {target_abi})\n") + f.write(f"set(VCPKG_CRT_LINKAGE {crt_linkage})\n") + f.write("set(VCPKG_LIBRARY_LINKAGE static)\n") + if toolset_version: + f.write(f"set(VCPKG_PLATFORM_TOOLSET_VERSION {toolset_version})\n") + cflags = ["/MP", "/DWIN32", "/D_WINDOWS"] + if enable_binskim: + cflags += [ + "/DWINAPI_FAMILY=100", + "/DWINVER=0x0A00", + "/D_WIN32_WINNT=0x0A00", + "/DNTDDI_VERSION=0x0A000000", + ] + ldflags = [] + if enable_binskim: + cflags += ["/guard:cf", "/Qspectre", "/W3"] + ldflags = ["/profile", "/DYNAMICBASE"] + elif enable_asan: + cflags.append("/fsanitize=address") + cxxflags = cflags.copy() + cxxflags.append("/Zc:__cplusplus") + if enable_exception: + cxxflags.append("/EHsc") + # MSVC doesn't have a specific flag to disable exceptions like /EHs-c- + # but relies on _HAS_EXCEPTIONS=0 and potentially other flags managed by ORT's main CMake. + # Vcpkg doesn't directly control this via a simple triplet flag AFAIK. + # ORT's CMake handles this via CMAKE_CXX_FLAGS adjustment. + if not enable_rtti: + cxxflags += ["/GR-", "/we4541"] + if cflags: + f.write(f'set(VCPKG_C_FLAGS "{" ".join(cflags)}")\n') + if cxxflags: + f.write(f'set(VCPKG_CXX_FLAGS "{" ".join(cxxflags)}")\n') + f.write( + "list(APPEND VCPKG_CMAKE_CONFIGURE_OPTIONS --compile-no-warning-as-error -DCMAKE_CXX_STANDARD=17)\n" + ) + if ldflags: + f.write(f'set(VCPKG_LINKER_FLAGS "{" ".join(ldflags)}")\n') + add_build_type(f, config) + add_port_configs( + f, enable_exception, False, enable_minimal_build + ) # Pass enable_minimal_build + + +def generate_linux_triplets(build_dir: str, configs: set[str]) -> None: """ Generate triplet files for Linux platforms. @@ -660,6 +693,7 @@ def generate_linux_triplets(build_dir: str) -> None: for target_abi in target_abis: generate_triplet_for_posix_platform( build_dir, + configs, "linux", enable_rtti, enable_exception, @@ -672,7 +706,7 @@ def generate_linux_triplets(build_dir: str) -> None: ) -def generate_macos_triplets(build_dir: str, osx_deployment_target: str) -> None: +def generate_macos_triplets(build_dir: str, configs: set[str], osx_deployment_target: str) -> None: """ Generate triplet files for macOS platforms. @@ -694,6 +728,7 @@ def generate_macos_triplets(build_dir: str, osx_deployment_target: str) -> None: for target_abi in target_abis: generate_triplet_for_posix_platform( build_dir, + configs, "osx", enable_rtti, enable_exception,