""" Unit tests for CMake script creation """ load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") load("//tools/build_defs:cmake_script.bzl", "create_cmake_script", "export_for_test") load("//tools/build_defs:cc_toolchain_util.bzl", "CxxFlagsInfo", "CxxToolsInfo") load("@foreign_cc_platform_utils//:os_info.bzl", "OSInfo") load("@foreign_cc_platform_utils//:tools.bzl", "CMAKE_COMMAND") def _absolutize_test(ctx): env = unittest.begin(ctx) cases = { "abs/a12": "abs/a12", "/abs/a12": "/abs/a12", "external/cmake/aaa": "$EXT_BUILD_ROOT/external/cmake/aaa", "-Lexternal/cmake/aaa": "-L$EXT_BUILD_ROOT/external/cmake/aaa", "ws/cmake/aaa": "$EXT_BUILD_ROOT/ws/cmake/aaa", "name=ws/cmake/aaa": "name=$EXT_BUILD_ROOT/ws/cmake/aaa", } for case in cases: res = export_for_test.absolutize("ws", case) asserts.equals(env, cases[case], res) unittest.end(env) def _tail_extraction_test(ctx): env = unittest.begin(ctx) res = export_for_test.tail_if_starts_with("absolutely", "abs") asserts.equals(env, "olutely", res) res = export_for_test.tail_if_starts_with("--option=value", "-option") asserts.equals(env, None, res) res = export_for_test.tail_if_starts_with("--option=value", "--option") asserts.equals(env, "=value", res) unittest.end(env) def _find_flag_value_test(ctx): env = unittest.begin(ctx) found_cases = [ ["--gcc_toolchain=/abc/def"], ["--gcc_toolchain =/abc/def"], ["--gcc_toolchain= /abc/def"], ["--gcc_toolchain = /abc/def"], [" --gcc_toolchain = /abc/def"], ["--gcc_toolchain", "=/abc/def"], ["--gcc_toolchain", "/abc/def"], ["-gcc_toolchain", "/abc/def"], ["-gcc_toolchain=/abc/def"], ["-gcc_toolchain = /abc/def"], ["--gcc_toolchain /abc/def"], ] for case in found_cases: res = export_for_test.find_flag_value(case, "gcc_toolchain") asserts.equals(env, "/abc/def", res, msg = "Not equals: " + str(case)) not_found_cases = [ ["--gcc_toolchainn=/abc/def"], ["--gcc_toolchain abc/def"], ] for case in not_found_cases: res = export_for_test.find_flag_value(case, "gcc_toolchain") asserts.false(env, "/abc/def" == res, msg = "Equals: " + str(case)) unittest.end(env) def _fill_crossfile_from_toolchain_test(ctx): env = unittest.begin(ctx) tools = CxxToolsInfo( cc = "some-cc-value", cxx = "external/cxx-value", cxx_linker_static = "cxx_linker_static", cxx_linker_executable = "ws/cxx_linker_executable", ) flags = CxxFlagsInfo( cc = ["-cc-flag", "-gcc_toolchain", "cc-toolchain"], cxx = ["--quoted=\"abc def\"", "--sysroot=/abc/sysroot", "--gcc_toolchain", "cxx-toolchain"], cxx_linker_shared = ["shared1", "shared2"], cxx_linker_static = ["static"], cxx_linker_executable = ["executable"], assemble = ["assemble"], ) os_info = OSInfo(is_unix = True, is_osx = False, is_win = False) res = export_for_test.fill_crossfile_from_toolchain("ws", os_info, tools, flags) system = res.pop("CMAKE_SYSTEM_NAME") asserts.true(env, system != None) expected = { "CMAKE_SYSROOT": "/abc/sysroot", "CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN": "cc-toolchain", "CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN": "cxx-toolchain", "CMAKE_C_COMPILER": "some-cc-value", "CMAKE_CXX_COMPILER": "$EXT_BUILD_ROOT/external/cxx-value", "CMAKE_AR": "cxx_linker_static", "CMAKE_CXX_LINK_EXECUTABLE": "$EXT_BUILD_ROOT/ws/cxx_linker_executable -o ", "CMAKE_C_FLAGS_INIT": "-cc-flag -gcc_toolchain cc-toolchain", "CMAKE_CXX_FLAGS_INIT": "--quoted=\\\"abc def\\\" --sysroot=/abc/sysroot --gcc_toolchain cxx-toolchain", "CMAKE_ASM_FLAGS_INIT": "assemble", "CMAKE_SHARED_LINKER_FLAGS_INIT": "shared1 shared2", "CMAKE_EXE_LINKER_FLAGS_INIT": "executable", } for key in expected: asserts.equals(env, expected[key], res[key]) unittest.end(env) def _move_dict_values_test(ctx): env = unittest.begin(ctx) target = { "CMAKE_C_COMPILER": "some-cc-value", "CMAKE_CXX_COMPILER": "$EXT_BUILD_ROOT/external/cxx-value", "CMAKE_C_FLAGS_INIT": "-cc-flag -gcc_toolchain cc-toolchain", "CMAKE_CXX_LINK_EXECUTABLE": "was", } source_env = { "CC": "sink-cc-value", "CXX": "sink-cxx-value", "CFLAGS": "--from-env", "CUSTOM": "YES", } source_cache = { "CMAKE_C_FLAGS": "--additional-flag", "CMAKE_ASM_FLAGS": "assemble", "CMAKE_CXX_LINK_EXECUTABLE": "became", "CUSTOM": "YES", } export_for_test.move_dict_values(target, source_env, export_for_test.CMAKE_ENV_VARS_FOR_CROSSTOOL) export_for_test.move_dict_values(target, source_cache, export_for_test.CMAKE_CACHE_ENTRIES_CROSSTOOL) expected_target = { "CMAKE_C_COMPILER": "sink-cc-value", "CMAKE_CXX_COMPILER": "sink-cxx-value", "CMAKE_C_FLAGS_INIT": "-cc-flag -gcc_toolchain cc-toolchain --from-env --additional-flag", "CMAKE_ASM_FLAGS_INIT": "assemble", "CMAKE_CXX_LINK_EXECUTABLE": "became", } for key in expected_target: asserts.equals(env, expected_target[key], target[key]) asserts.equals(env, "YES", source_env["CUSTOM"]) asserts.equals(env, "YES", source_cache["CUSTOM"]) asserts.equals(env, 1, len(source_env)) asserts.equals(env, 1, len(source_cache)) unittest.end(env) def _reverse_descriptor_dict_test(ctx): env = unittest.begin(ctx) res = export_for_test.reverse_descriptor_dict(export_for_test.CMAKE_CACHE_ENTRIES_CROSSTOOL) expected = { "CMAKE_SYSTEM_NAME": struct(value = "CMAKE_SYSTEM_NAME", replace = True), "CMAKE_AR": struct(value = "CMAKE_AR", replace = True), "CMAKE_CXX_LINK_EXECUTABLE": struct(value = "CMAKE_CXX_LINK_EXECUTABLE", replace = True), "CMAKE_C_FLAGS_INIT": struct(value = "CMAKE_C_FLAGS", replace = False), "CMAKE_CXX_FLAGS_INIT": struct(value = "CMAKE_CXX_FLAGS", replace = False), "CMAKE_ASM_FLAGS_INIT": struct(value = "CMAKE_ASM_FLAGS", replace = False), "CMAKE_STATIC_LINKER_FLAGS_INIT": struct(value = "CMAKE_STATIC_LINKER_FLAGS", replace = False), "CMAKE_SHARED_LINKER_FLAGS_INIT": struct(value = "CMAKE_SHARED_LINKER_FLAGS", replace = False), "CMAKE_EXE_LINKER_FLAGS_INIT": struct(value = "CMAKE_EXE_LINKER_FLAGS", replace = False), } for key in expected: asserts.equals(env, expected[key], res[key]) unittest.end(env) def _merge_toolchain_and_user_values_test(ctx): env = unittest.begin(ctx) target = { "CMAKE_C_COMPILER": "some-cc-value", "CMAKE_CXX_COMPILER": "$EXT_BUILD_ROOT/external/cxx-value", "CMAKE_C_FLAGS_INIT": "-cc-flag -gcc_toolchain cc-toolchain", "CMAKE_CXX_FLAGS_INIT": "-ccx-flag", "CMAKE_CXX_LINK_EXECUTABLE": "was", } source_cache = { "CMAKE_C_FLAGS": "--additional-flag", "CMAKE_ASM_FLAGS": "assemble", "CMAKE_CXX_LINK_EXECUTABLE": "became", "CUSTOM": "YES", } res = export_for_test.merge_toolchain_and_user_values(target, source_cache, export_for_test.CMAKE_CACHE_ENTRIES_CROSSTOOL) expected_target = { "CMAKE_C_FLAGS": "-cc-flag -gcc_toolchain cc-toolchain --additional-flag", "CMAKE_CXX_FLAGS": "-ccx-flag", "CMAKE_ASM_FLAGS": "assemble", "CMAKE_CXX_LINK_EXECUTABLE": "became", "CUSTOM": "YES", } for key in expected_target: asserts.equals(env, expected_target[key], res[key]) unittest.end(env) def _merge_flag_values_no_toolchain_file_test(ctx): env = unittest.begin(ctx) tools = CxxToolsInfo( cc = "/usr/bin/gcc", cxx = "/usr/bin/gcc", cxx_linker_static = "/usr/bin/ar", cxx_linker_executable = "/usr/bin/gcc", ) flags = CxxFlagsInfo( cc = [], cxx = ["foo=\"bar\""], cxx_linker_shared = [], cxx_linker_static = [], cxx_linker_executable = [], assemble = [], ) user_env = {} user_cache = { "CMAKE_CXX_FLAGS": "-Fbat", } os_info = OSInfo(is_unix = True, is_osx = False, is_win = False) script = create_cmake_script("ws", os_info, tools, flags, "test_rule", "external/test_rule", True, user_cache, user_env, []) expected = """CC=\"/usr/bin/gcc\" CXX=\"/usr/bin/gcc\" CXXFLAGS=\"foo=\\\"bar\\\" -Fbat" {cmake} -DCMAKE_AR=\"/usr/bin/ar\" -DCMAKE_PREFIX_PATH=\"$EXT_BUILD_DEPS\" -DCMAKE_INSTALL_PREFIX=\"test_rule\" -DCMAKE_BUILD_TYPE=\"DEBUG\" $EXT_BUILD_ROOT/external/test_rule""" asserts.equals(env, expected.format(cmake = CMAKE_COMMAND), script) unittest.end(env) def _create_min_cmake_script_no_toolchain_file_test(ctx): env = unittest.begin(ctx) tools = CxxToolsInfo( cc = "/usr/bin/gcc", cxx = "/usr/bin/gcc", cxx_linker_static = "/usr/bin/ar", cxx_linker_executable = "/usr/bin/gcc", ) flags = CxxFlagsInfo( cc = ["-U_FORTIFY_SOURCE", "-fstack-protector", "-Wall"], cxx = ["-U_FORTIFY_SOURCE", "-fstack-protector", "-Wall"], cxx_linker_shared = ["-shared", "-fuse-ld=gold"], cxx_linker_static = ["static"], cxx_linker_executable = ["-fuse-ld=gold", "-Wl", "-no-as-needed"], assemble = ["-U_FORTIFY_SOURCE", "-fstack-protector", "-Wall"], ) user_env = {} user_cache = { "NOFORTRAN": "on", "CMAKE_PREFIX_PATH": "/abc/def", } os_info = OSInfo(is_unix = True, is_osx = False, is_win = False) script = create_cmake_script("ws", os_info, tools, flags, "test_rule", "external/test_rule", True, user_cache, user_env, ["-GNinja"]) expected = "CC=\"/usr/bin/gcc\" CXX=\"/usr/bin/gcc\" CFLAGS=\"-U_FORTIFY_SOURCE -fstack-protector -Wall\" CXXFLAGS=\"-U_FORTIFY_SOURCE -fstack-protector -Wall\" ASMFLAGS=\"-U_FORTIFY_SOURCE -fstack-protector -Wall\" {cmake} -DCMAKE_AR=\"/usr/bin/ar\" -DCMAKE_SHARED_LINKER_FLAGS=\"-shared -fuse-ld=gold\" -DCMAKE_EXE_LINKER_FLAGS=\"-fuse-ld=gold -Wl -no-as-needed\" -DNOFORTRAN=\"on\" -DCMAKE_PREFIX_PATH=\"$EXT_BUILD_DEPS;/abc/def\" -DCMAKE_INSTALL_PREFIX=\"test_rule\" -DCMAKE_BUILD_TYPE=\"DEBUG\" -GNinja $EXT_BUILD_ROOT/external/test_rule" asserts.equals(env, expected.format(cmake = CMAKE_COMMAND), script) unittest.end(env) def _create_min_cmake_script_toolchain_file_test(ctx): env = unittest.begin(ctx) tools = CxxToolsInfo( cc = "/usr/bin/gcc", cxx = "/usr/bin/gcc", cxx_linker_static = "/usr/bin/ar", cxx_linker_executable = "/usr/bin/gcc", ) flags = CxxFlagsInfo( cc = ["-U_FORTIFY_SOURCE", "-fstack-protector", "-Wall"], cxx = ["-U_FORTIFY_SOURCE", "-fstack-protector", "-Wall"], cxx_linker_shared = ["-shared", "-fuse-ld=gold"], cxx_linker_static = ["static"], cxx_linker_executable = ["-fuse-ld=gold", "-Wl", "-no-as-needed"], assemble = ["-U_FORTIFY_SOURCE", "-fstack-protector", "-Wall"], ) user_env = {} user_cache = { "NOFORTRAN": "on", } os_info = OSInfo(is_unix = True, is_osx = False, is_win = False) script = create_cmake_script("ws", os_info, tools, flags, "test_rule", "external/test_rule", False, user_cache, user_env, ["-GNinja"]) expected = """cat > crosstool_bazel.cmake < crosstool_bazel.cmake <