<commit_before># Allow to easily build native executable.
# Useful for cross compilation.

if(NOT DEFINED __IS_NATIVE_BUILD)
	# Check if we are in a native build or not.
	set(__IS_NATIVE_BUILD 0 CACHE INTERNAL "Indicate if this is a native build")
endif()

if(__IS_NATIVE_BUILD AND CMAKE_CROSSCOMPILING)
	message(FATAL_ERROR "A native build cannot be cross compiled")
endif()

# It is imperative that NATIVE_BUILD_DIR be in the cache.
set(NATIVE_BUILD_DIR "${CMAKE_BINARY_DIR}/native" CACHE PATH "The path of the native build directory")

function(add_native_executable NAME)
	if(__IS_NATIVE_BUILD)
		add_executable(${NAME} EXCLUDE_FROM_ALL ${ARGN})
	else()
		set(NATIVE_TARGET "${NAME}")
		file(RELATIVE_PATH RELATIVE_PATH "${CMAKE_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
		if(RELATIVE_PATH)
			string(PREPEND NATIVE_TARGET "${RELATIVE_PATH}/")
		endif()
		set(NATIVE_BINARY "${NATIVE_BUILD_DIR}/${NATIVE_TARGET}")

		# We create a symlink because cmake craps itself if the imported
		# executable has the same name as the executable itself.
		# https://cmake.org/pipermail/cmake/2019-May/069480.html
		add_custom_command(
			OUTPUT "native-${NAME}"
			COMMENT "Building native ${NATIVE_TARGET}"
			COMMAND ${CMAKE_COMMAND} --build "${NATIVE_BUILD_DIR}" --target "${NAME}"
			COMMAND ${CMAKE_COMMAND} -E create_symlink "${NATIVE_BINARY}" "native-${NAME}"
			DEPENDS native-cmake-build ${ARGN}
			VERBATIM USES_TERMINAL
		)

		add_executable(${NAME} IMPORTED GLOBAL)
		set_target_properties(${NAME} PROPERTIES IMPORTED_LOCATION "${NATIVE_BINARY}")

		# This obviously cannot depend on a file for some mysterious reasons only<commit_msg>[CMAKE] Fix the build with Xcode as a generator<commit_after># Allow to easily build native executable.
# Useful for cross compilation.

if(NOT DEFINED __IS_NATIVE_BUILD)
	# Check if we are in a native build or not.
	set(__IS_NATIVE_BUILD 0 CACHE INTERNAL "Indicate if this is a native build")
endif()

if(__IS_NATIVE_BUILD AND CMAKE_CROSSCOMPILING)
	message(FATAL_ERROR "A native build cannot be cross compiled")
endif()

# It is imperative that NATIVE_BUILD_DIR be in the cache.
set(NATIVE_BUILD_DIR "${CMAKE_BINARY_DIR}/native" CACHE PATH "The path of the native build directory")

function(add_native_executable NAME)
	if(__IS_NATIVE_BUILD)
		add_executable(${NAME} EXCLUDE_FROM_ALL ${ARGN})
		# Multi-configuration generators (VS, Xcode) append a per-configuration
		# subdirectory to the specified directory unless the 
		# `RUNTIME_OUTPUT_DIRECTORY` property is defined using a generator
		# expression.
		# Since we don't care about the build configuration for native
		# executables, we can simply drop this subdirectory.
		# Doing so ensure that the path to the binary can always be retrieved.
		set_target_properties(${NAME} PROPERTIES
			RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/$<0:>"
		)
	else()
		set(NATIVE_TARGET "${NAME}")
		file(RELATIVE_PATH RELATIVE_PATH "${CMAKE_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}")
		if(RELATIVE_PATH)
			string(PREPEND NATIVE_TARGET "${RELATIVE_PATH}/")
		endif()
		set(NATIVE_BINARY "${NATIVE_BUILD_DIR}/${NATIVE_TARGET}")

		# We create a symlink because cmake craps itself if the imported
		# executable has the same name as the executable itself.
		# https://cmake.org/pipermail/cmake/2019-May/069480.html
		add_custom_command(
			OUTPUT "native-${NAME}"
			COMMENT "Building native ${NATIVE_TARGET}"
			COMMAND ${CMAKE_COMMAND} --build "${NATIVE_BUILD_DIR}" --target "${NAME}"
			COMMAND ${CMAKE_COMMAND} -E create_symlink "${NATIVE_BINARY}" "native-${NAME}"
			DEPENDS native-cmake-build ${ARGN}
			VERBATIM USES_TERMINAL
		)

		add_executable(${NAME} IMPORTED GLOBAL)
		set_target_properties(${NAME} PROPERTIES IMPORTED_LOCATION "${NATIVE_BINARY}")

		# This obviously cannot depend on a file for some mysterious reasons only
{'fraction_non_alphanumeric': 0.09966084007304983, 'fraction_numerical': 0.006000521784503, 'mean_word_length': 5.465430016863406, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6942725', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1264, 'n_words': 371}