diff --git a/cmake/modules/NativeExecutable.cmake b/cmake/modules/NativeExecutable.cmake --- a/cmake/modules/NativeExecutable.cmake +++ b/cmake/modules/NativeExecutable.cmake @@ -1,18 +1,25 @@ # Allow to easily build native executable. # Useful for cross compilation. +# Check if we are in a native build or not. +set(__IS_NATIVE_BUILD 0 CACHE INTERNAL "Indicate if this is a native build") +if(__IS_NATIVE_BUILD AND CMAKE_CROSSCOMPILING) + message(FATAL_ERROR "A native build cannot be cross compiled") +endif() + # If we are cross compiling, create a directory for native build. -set(NATIVE_BUILD_DIR "${CMAKE_BINARY_DIR}/native" CACHE PATH "Path to the native build directory") -set(NATIVE_BINARY_DIR "${NATIVE_BUILD_DIR}/bin" CACHE PATH "Path to the native binary directory") +set(NATIVE_BUILD_DIR "${CMAKE_BINARY_DIR}/native") +set(NATIVE_BINARY_DIR "${NATIVE_BUILD_DIR}/bin") set(NATIVE_BUILD_TARGET "${NATIVE_BUILD_DIR}/CMakeCache.txt") -if(CMAKE_CROSSCOMPILING AND NOT TARGET native-cmake-build) +if(NOT __IS_NATIVE_BUILD AND NOT TARGET native-cmake-build) file(MAKE_DIRECTORY ${NATIVE_BUILD_DIR}) add_custom_command( OUTPUT ${NATIVE_BUILD_TARGET} COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" "${CMAKE_SOURCE_DIR}" + "-D__IS_NATIVE_BUILD=1" "-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}" "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY:PATH=${NATIVE_BINARY_DIR}" # Don't require native third party dependencies we don't need. @@ -29,7 +36,7 @@ endif() macro(add_native_executable NAME) - if(CMAKE_CROSSCOMPILING) + if(NOT __IS_NATIVE_BUILD) set(NATIVE_BINARY "${NATIVE_BINARY_DIR}/${NAME}") add_custom_target("build-native-${NAME}" COMMAND ${CMAKE_COMMAND} @@ -46,11 +53,11 @@ set_property(TARGET ${NAME} PROPERTY IMPORTED_LOCATION ${NATIVE_BINARY}) else() add_executable(${NAME} EXCLUDE_FROM_ALL ${ARGN}) - endif(CMAKE_CROSSCOMPILING) + endif() endmacro(add_native_executable) function(native_target_include_directories) - if(NOT CMAKE_CROSSCOMPILING) + if(__IS_NATIVE_BUILD) target_include_directories(${ARGN}) - endif(NOT CMAKE_CROSSCOMPILING) + endif() endfunction(native_target_include_directories)