Skip to content

Commit 7fe2a0e

Browse files
committed
CMake: check whether check_c_source_runs() works.
That's the simplest way to check whether we can use check_c_source_runs() to test for a suitable snprintf; it's easier than trying to find out the target instruction set architecture and comparing it with the host instruction set architecture, as CMake doesn't provide any mechanism to provide the target instruction set architecture, on all platforms, in a form that can be compared with the host instruction set architecture, and even if the target is different, we may be able to run code for that instruction set architecture if, for example, it's a 32-bit version of the instruction set architecture on which the build is being done, or if there's a binary emulator.
1 parent b170946 commit 7fe2a0e

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

CMakeLists.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,23 @@ endif(MSVC)
409409
# on an IA-32 or x86-64 Linux box for an Arm embedded Linux box,
410410
# or building Arm code on an IA-32 or x86-64 Windows box.
411411
#
412-
# At least for the Windows case, people may do those builds by
413-
# setting the target with th -A flag to CMake; that causes
414-
# CMAKE_GENERATOR_PLATFORM to be set to the target. If
415-
# CMAKE_GENERATOR_PLATFORM is set, compare it with
416-
# CMAKE_HOST_SYSTEM_PROCESSOR and, if they're not equal, set
417-
# CMAKE_CROSSCOMPILING to TRUE.
418-
#
419-
if (CMAKE_GENERATOR_PLATFORM AND
420-
NOT CMAKE_GENERATOR_PLATFORM STREQUAL CMAKE_HOST_SYSTEM_PROCESSOR)
421-
set(CMAKE_CROSSCOMPILING TRUE)
412+
# So just test whether check_c_source_runs() on a trivial program
413+
# works; if not, it's probably because the generated code won't
414+
# run on the platform on which we're running.
415+
#
416+
include(CheckCSourceRuns)
417+
if (NOT CMAKE_CROSSCOMPILING)
418+
check_c_source_runs("
419+
int main()
420+
{
421+
return 0;
422+
}
423+
"
424+
CHECK_C_SOURCE_RUNS_WORKS
425+
)
426+
if (NOT CHECK_C_SOURCE_RUNS_WORKS)
427+
set(CMAKE_CROSSCOMPILING TRUE)
428+
endif()
422429
endif()
423430

424431
###################################################################
@@ -529,7 +536,6 @@ if (NOT CMAKE_CROSSCOMPILING)
529536
#
530537
# Require a proof of suitable snprintf(3), same as in Autoconf.
531538
#
532-
include(CheckCSourceRuns)
533539
check_c_source_runs("
534540
#include <stdio.h>
535541
#include <string.h>

0 commit comments

Comments
 (0)