@@ -45,6 +45,7 @@ def create_cmake_script(
4545 target_os ,
4646 target_arch ,
4747 host_os ,
48+ host_arch ,
4849 generator ,
4950 cmake_path ,
5051 tools ,
@@ -68,6 +69,7 @@ def create_cmake_script(
6869 target_os: The target OS for the build
6970 target_arch: The target arch for the build
7071 host_os: The execution OS for the build
72+ host_arch: The execution arch for the build
7173 generator: The generator target for cmake to use
7274 cmake_path: The path to the cmake executable
7375 tools: cc_toolchain tools (CxxToolsInfo)
@@ -93,6 +95,23 @@ def create_cmake_script(
9395 toolchain_dict = _fill_crossfile_from_toolchain (workspace_name , tools , flags , target_os )
9496 params = None
9597
98+ # Avoid CMake passing the wrong linker flags when cross compiling
99+ # by setting CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR,
100+ # see https://github.com/bazel-contrib/rules_foreign_cc/issues/289,
101+ # and https://github.com/bazel-contrib/rules_foreign_cc/pull/1062
102+ # note: these parameters must be set in the toolchain file, not just in the
103+ # cache, and CMAKE_SYSTEM_PROCESSOR is ignored unless CMAKE_SYSTEM_NAME is
104+ # also set.
105+ if target_os == "unknown" :
106+ # buildifier: disable=print
107+ print ("target_os is unknown, please update foreign_cc/private/framework/platform.bzl and foreign_cc/private/cmake_script.bzl; triggered by" , current_label )
108+ elif target_arch == "unknown" :
109+ # buildifier: disable=print
110+ print ("target_arch is unknown, please update foreign_cc/private/framework/platform.bzl and foreign_cc/private/cmake_script.bzl; triggered by" , current_label )
111+ elif target_os != host_os or target_arch != host_arch :
112+ toolchain_dict .update (_TARGET_OS_PARAMS .get (target_os , {}))
113+ toolchain_dict .update (_TARGET_ARCH_PARAMS .get (target_arch , {}))
114+
96115 keys_with_empty_values_in_user_cache = [key for key in user_cache if user_cache .get (key ) == "" ]
97116
98117 if no_toolchain_file :
@@ -123,17 +142,6 @@ def create_cmake_script(
123142 if not params .cache .get ("CMAKE_RANLIB" ):
124143 params .cache .update ({"CMAKE_RANLIB" : "" })
125144
126- # Avoid CMake passing the wrong linker flags when cross compiling
127- # by setting CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR,
128- # see https://github.com/bazel-contrib/rules_foreign_cc/issues/289,
129- # and https://github.com/bazel-contrib/rules_foreign_cc/pull/1062
130- if target_os == "unknown" :
131- # buildifier: disable=print
132- print ("target_os is unknown, please update foreign_cc/private/framework/platform.bzl and foreign_cc/private/cmake_script.bzl; triggered by" , current_label )
133- elif target_os != host_os :
134- params .cache .update (_TARGET_OS_PARAMS .get (target_os , {}))
135- params .cache .update (_TARGET_ARCH_PARAMS .get (target_arch , {}))
136-
137145 set_env_vars = [
138146 "export {}=\" {}\" " .format (key , _escape_dquote_bash (params .env [key ]))
139147 for key in params .env
@@ -194,6 +202,7 @@ _CMAKE_ENV_VARS_FOR_CROSSTOOL = {
194202}
195203
196204_CMAKE_CACHE_ENTRIES_CROSSTOOL = {
205+ "ANDROID" : struct (value = "ANDROID" , replace = False ),
197206 "CMAKE_AR" : struct (value = "CMAKE_AR" , replace = True ),
198207 "CMAKE_ASM_FLAGS" : struct (value = "CMAKE_ASM_FLAGS_INIT" , replace = False ),
199208 "CMAKE_CXX_ARCHIVE_CREATE" : struct (value = "CMAKE_CXX_ARCHIVE_CREATE" , replace = False ),
@@ -206,6 +215,8 @@ _CMAKE_CACHE_ENTRIES_CROSSTOOL = {
206215 "CMAKE_RANLIB" : struct (value = "CMAKE_RANLIB" , replace = True ),
207216 "CMAKE_SHARED_LINKER_FLAGS" : struct (value = "CMAKE_SHARED_LINKER_FLAGS_INIT" , replace = False ),
208217 "CMAKE_STATIC_LINKER_FLAGS" : struct (value = "CMAKE_STATIC_LINKER_FLAGS_INIT" , replace = False ),
218+ "CMAKE_SYSTEM_NAME" : struct (value = "CMAKE_SYSTEM_NAME" , replace = False ),
219+ "CMAKE_SYSTEM_PROCESSOR" : struct (value = "CMAKE_SYSTEM_PROCESSOR" , replace = False ),
209220}
210221
211222def _create_crosstool_file_text (toolchain_dict , user_cache , user_env ):
0 commit comments