<commit_before>#
# file: CMakeLists.txt
#
# This is the main CMakeLists.txt for distortos
#
# author: Copyright (C) 2018 <NAME> http://www.distortec.com http://www.freddiechopin.info
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

cmake_minimum_required(VERSION 3.1)
project(distortos)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(distortos-utilities)

#-----------------------------------------------------------------------------------------------------------------------
# load variables from selectedConfiguration.mk
#-----------------------------------------------------------------------------------------------------------------------

if(NOT EXISTS ${CMAKE_SOURCE_DIR}/selectedConfiguration.mk)
	message(FATAL_ERROR "${CMAKE_SOURCE_DIR}/selectedConfiguration.mk file does not exist; Please run first "
	"'make configure [CONFIG_PATH=<path-to-distortosConfiguration.mk>]'")
endif()
# force CMake to rerun if selectedConfiguration.mk file changes
configure_file(${CMAKE_SOURCE_DIR}/selectedConfiguration.mk selectedConfiguration.mk)
loadConfiguration(${CMAKE_SOURCE_DIR}/selectedConfiguration.mk)

#-----------------------------------------------------------------------------------------------------------------------
# load variables from selected distortosConfiguration.mk
#-----------------------------------------------------------------------------------------------------------------------

if(NOT EXISTS ${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION})
	message(FATAL_ERROR "Selected configuration file '${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION}' does not "
	"exist")
endif()
# force CMake to rerun if selected distortosConfiguration.mk file changes
configure_file(${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION} ${CONFIG_SELECTED_CONFIGURATION})
loadConfiguration(${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION})

#-----------------------------------------------------------------------------------------------------------------------
# generate distortosConfiguration.h
#-----------------------------------------------------------------------------------------------------------------------

add_custom_command(OUTPUT include/distortos/distortosConfiguration.h
		COMMAND ${CMAKE_COMMAND} -E make_directory include/distortos
		COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/makeDistortosConfiguration.sh
		${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION} > include/distortos/distortosConfiguration.h
		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/makeDistortosConfiguration.sh
		DEPENDS ${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION}
		DEPENDS ${CMAKE_SOURCE_DIR}/selectedConfiguration.mk
		USES_TERMINAL)
add_custom_target(distortosConfiguration_h ALL DEPENDS include/distortos/distortosConfiguration.h)

#-----------------------------------------------------------------------------------------------------------------------
# preprocess linker script
#-----------------------------------------------------------------------------------------------------------------------

if(NOT CONFIG_LDSCRIPT)
	message(FATAL_ERROR "Non-board configurations are not supported")
endif()
set(RAW_LDSCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/${CONFIG_LDSCRIPT}")
if(NOT EXISTS ${RAW_LDSCRIPT})
	message(FATAL_ERROR "Linker script file '${RAW_LDSCRIPT}' does not exist")
endif()
get_filename_component(LDSCRIPT ${RAW_LDSCRIPT} NAME_WE)
set(LDSCRIPT "${LDSCRIPT}.preprocessed.ld")
add_custom_command(OUTPUT ${LDSCRIPT}
		COMMAND ${CMAKE_CXX_COMPILER} -nostdinc -undef -C -E -P -x assembler-with-cpp
		-I${CMAKE_CURRENT_BINARY_DIR}/include ${RAW_LDSCRIPT} -o ${LDSCRIPT}
		DEPENDS ${RAW_LDSCRIPT}
		DEPENDS include/distortos/distortosConfiguration.h
		USES_TERMINAL)
add_custom_target(ldscript ALL DEPENDS ${LDSCRIPT})

#-----------------------------------------------------------------------------------------------------------------------
# generate distortos static library
#-----------------------------------------------------------------------------------------------------------------------

add_library(distortos STATIC "")
set_target_properties(distortos PROPERTIES
		CXX_STANDARD 11
		CXX_STANDARD_REQUIRED ON)
target_compile_options(distortos PRIVATE
		-Wall
		-Wextra
		-Wshadow
		${CONFIG_ARCHITECTURE_FLAGS}
		${CONFIG_DEBUGGING_INFORMATION_COMPILATION}
		${CONFIG_BUILD_OPTIMIZATION}
		${CONFIG_LINK_TIME_OPTIMIZATION_COMPILATION}
		${CONFIG_STATIC_DESTRUCTORS_RUN_TIME_REGISTRATION}
		-ffunction-sections
		-fdata-sections
		-fno-rtti
		-fno-exceptions
		${CONFIG_ASSERT})
target_include_directories(distortos PRIVATE
		${CMAKE_CURRENT_BINARY_DIR}/include
		include
		${CONFIG_ARCHITECTURE_INCLUDES}
		${CONFIG_CHIP_INCLUDES}
		${CONFIG_BOARD_INCLUDES})
add_dependencies(distortos distortosConfiguration_h)

include(source/distortos-sources.cmake)

file(GLOB_RECURSE distortosBoardSourcesFiles FOLLOW_SYMLINKS LIST_DIRECTORIES false "distortos-board-sources.cmake")
foreach(distortosBoardSources ${distortosBoardSourcesFiles})
	include(${distortosBoardSources})
endforeach()

#-----------------------------------------------------------------------------------------------------------------------
# distortos interface library
#-----------------------------------------------------------------------------------------------------------------------

add_library(distortosInterface INTERFACE)
target_compile_options(distortosInterface INTERFACE
		-Wall
		-Wextra
		-Wshadow
		${CONFIG_ARCHITECTURE_FLAGS}
		${CONFIG_DEBUGGING_INFORMATION_COMPILATION}
		${CONFIG_BUILD_OPTIMIZATION}
		${CONFIG_LINK_TIME_OPTIMIZATION_COMPILATION}
		${CONFIG_STATIC_DESTRUCTORS_RUN_TIME_REGISTRATION}
		-ffunction-sections
		-fdata-sections
		-fno-rtti
		-fno-exceptions
		${CONFIG_ASSERT})
target_include_directories(distortosInterface INTERFACE
		${CMAKE_CURRENT_BINARY_DIR}/include
		include
		${CONFIG_ARCHITECTURE_INCLUDES}
		${CONFIG_CHIP_INCLUDES}
		${CONFIG_BOARD_INCLUDES})
target_link_libraries(distortosInterface INTERFACE
		${CONFIG_ARCHITECTURE_FLAGS}
		${CONFIG_DEBUGGING_INFORMATION_LINKING}
		${CONFIG_BUILD_OPTIMIZATION}
		${CONFIG_LINK_TIME_OPTIMIZATION_LINKING}
		-Wl,--gc-sections
		-L${CMAKE_CURRENT_BINARY_DIR}
		-T${LDSCRIPT}
		-Wl,--whole-archive -ldistortos -Wl,--no-whole-archive)
add_dependencies(distortosInterface distortos ldscript)

#-----------------------------------------------------------------------------------------------------------------------
# distortos::distortos alias library
#-----------------------------------------------------------------------------------------------------------------------

add_library(distortos::distortos ALIAS distortosInterface)

#-----------------------------------------------------------------------------------------------------------------------
# distortos-doxygen
#-----------------------------------------------------------------------------------------------------------------------

doxygen(INPUT ${CMAKE_CURRENT_SOURCE_DIR}/documentation ${CMAKE_CURRENT_BINARY_DIR}/include
		${CMAKE_CURRENT_SOURCE_DIR}/include
		INCLUDE_PATH ${CMAKE_CURRENT_BINARY_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include)

get_target_property(doxygenCompileOptions distortos COMPILE_OPTIONS)
# for now C++11 standard with GNU extensions is hardcoded here...
execute_process(COMMAND ${CMAKE_COMMAND} -E echo ""
		COMMAND ${CMAKE_CXX_COMPILER} ${doxygenCompileOptions} -std=gnu++11 -E -P -dD -x c++ -
		OUTPUT_VARIABLE preprocessedOutput)
string(REGEX REPLACE "[\r\n]+" ";" preprocessedOutput "${preprocessedOutput}")
foreach(predefinedEntry ${preprocessedOutput})<commit_msg>Make names of variables related to linker script more consistent<commit_after>#
# file: CMakeLists.txt
#
# This is the main CMakeLists.txt for distortos
#
# author: Copyright (C) 2018 <NAME> http://www.distortec.com http://www.freddiechopin.info
#
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

cmake_minimum_required(VERSION 3.1)
project(distortos)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(distortos-utilities)

#-----------------------------------------------------------------------------------------------------------------------
# load variables from selectedConfiguration.mk
#-----------------------------------------------------------------------------------------------------------------------

if(NOT EXISTS ${CMAKE_SOURCE_DIR}/selectedConfiguration.mk)
	message(FATAL_ERROR "${CMAKE_SOURCE_DIR}/selectedConfiguration.mk file does not exist; Please run first "
	"'make configure [CONFIG_PATH=<path-to-distortosConfiguration.mk>]'")
endif()
# force CMake to rerun if selectedConfiguration.mk file changes
configure_file(${CMAKE_SOURCE_DIR}/selectedConfiguration.mk selectedConfiguration.mk)
loadConfiguration(${CMAKE_SOURCE_DIR}/selectedConfiguration.mk)

#-----------------------------------------------------------------------------------------------------------------------
# load variables from selected distortosConfiguration.mk
#-----------------------------------------------------------------------------------------------------------------------

if(NOT EXISTS ${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION})
	message(FATAL_ERROR "Selected configuration file '${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION}' does not "
	"exist")
endif()
# force CMake to rerun if selected distortosConfiguration.mk file changes
configure_file(${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION} ${CONFIG_SELECTED_CONFIGURATION})
loadConfiguration(${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION})

#-----------------------------------------------------------------------------------------------------------------------
# generate distortosConfiguration.h
#-----------------------------------------------------------------------------------------------------------------------

add_custom_command(OUTPUT include/distortos/distortosConfiguration.h
		COMMAND ${CMAKE_COMMAND} -E make_directory include/distortos
		COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/makeDistortosConfiguration.sh
		${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION} > include/distortos/distortosConfiguration.h
		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/makeDistortosConfiguration.sh
		DEPENDS ${CMAKE_SOURCE_DIR}/${CONFIG_SELECTED_CONFIGURATION}
		DEPENDS ${CMAKE_SOURCE_DIR}/selectedConfiguration.mk
		USES_TERMINAL)
add_custom_target(distortosConfiguration_h ALL DEPENDS include/distortos/distortosConfiguration.h)

#-----------------------------------------------------------------------------------------------------------------------
# preprocess linker script
#-----------------------------------------------------------------------------------------------------------------------

if(NOT CONFIG_LDSCRIPT)
	message(FATAL_ERROR "Non-board configurations are not supported")
endif()
set(LDSCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/${CONFIG_LDSCRIPT}")
if(NOT EXISTS ${LDSCRIPT})
	message(FATAL_ERROR "Linker script file '${LDSCRIPT}' does not exist")
endif()
get_filename_component(PREPROCESSED_LDSCRIPT ${LDSCRIPT} NAME_WE)
set(PREPROCESSED_LDSCRIPT "${PREPROCESSED_LDSCRIPT}.preprocessed.ld")
add_custom_command(OUTPUT ${PREPROCESSED_LDSCRIPT}
		COMMAND ${CMAKE_CXX_COMPILER} -nostdinc -undef -C -E -P -x assembler-with-cpp
		-I${CMAKE_CURRENT_BINARY_DIR}/include ${LDSCRIPT} -o ${PREPROCESSED_LDSCRIPT}
		DEPENDS ${LDSCRIPT}
		DEPENDS include/distortos/distortosConfiguration.h
		USES_TERMINAL)
add_custom_target(ldscript ALL DEPENDS ${PREPROCESSED_LDSCRIPT})

#-----------------------------------------------------------------------------------------------------------------------
# generate distortos static library
#-----------------------------------------------------------------------------------------------------------------------

add_library(distortos STATIC "")
set_target_properties(distortos PROPERTIES
		CXX_STANDARD 11
		CXX_STANDARD_REQUIRED ON)
target_compile_options(distortos PRIVATE
		-Wall
		-Wextra
		-Wshadow
		${CONFIG_ARCHITECTURE_FLAGS}
		${CONFIG_DEBUGGING_INFORMATION_COMPILATION}
		${CONFIG_BUILD_OPTIMIZATION}
		${CONFIG_LINK_TIME_OPTIMIZATION_COMPILATION}
		${CONFIG_STATIC_DESTRUCTORS_RUN_TIME_REGISTRATION}
		-ffunction-sections
		-fdata-sections
		-fno-rtti
		-fno-exceptions
		${CONFIG_ASSERT})
target_include_directories(distortos PRIVATE
		${CMAKE_CURRENT_BINARY_DIR}/include
		include
		${CONFIG_ARCHITECTURE_INCLUDES}
		${CONFIG_CHIP_INCLUDES}
		${CONFIG_BOARD_INCLUDES})
add_dependencies(distortos distortosConfiguration_h)

include(source/distortos-sources.cmake)

file(GLOB_RECURSE distortosBoardSourcesFiles FOLLOW_SYMLINKS LIST_DIRECTORIES false "distortos-board-sources.cmake")
foreach(distortosBoardSources ${distortosBoardSourcesFiles})
	include(${distortosBoardSources})
endforeach()

#-----------------------------------------------------------------------------------------------------------------------
# distortos interface library
#-----------------------------------------------------------------------------------------------------------------------

add_library(distortosInterface INTERFACE)
target_compile_options(distortosInterface INTERFACE
		-Wall
		-Wextra
		-Wshadow
		${CONFIG_ARCHITECTURE_FLAGS}
		${CONFIG_DEBUGGING_INFORMATION_COMPILATION}
		${CONFIG_BUILD_OPTIMIZATION}
		${CONFIG_LINK_TIME_OPTIMIZATION_COMPILATION}
		${CONFIG_STATIC_DESTRUCTORS_RUN_TIME_REGISTRATION}
		-ffunction-sections
		-fdata-sections
		-fno-rtti
		-fno-exceptions
		${CONFIG_ASSERT})
target_include_directories(distortosInterface INTERFACE
		${CMAKE_CURRENT_BINARY_DIR}/include
		include
		${CONFIG_ARCHITECTURE_INCLUDES}
		${CONFIG_CHIP_INCLUDES}
		${CONFIG_BOARD_INCLUDES})
target_link_libraries(distortosInterface INTERFACE
		${CONFIG_ARCHITECTURE_FLAGS}
		${CONFIG_DEBUGGING_INFORMATION_LINKING}
		${CONFIG_BUILD_OPTIMIZATION}
		${CONFIG_LINK_TIME_OPTIMIZATION_LINKING}
		-Wl,--gc-sections
		-L${CMAKE_CURRENT_BINARY_DIR}
		-T${PREPROCESSED_LDSCRIPT}
		-Wl,--whole-archive -ldistortos -Wl,--no-whole-archive)
add_dependencies(distortosInterface distortos ldscript)

#-----------------------------------------------------------------------------------------------------------------------
# distortos::distortos alias library
#-----------------------------------------------------------------------------------------------------------------------

add_library(distortos::distortos ALIAS distortosInterface)

#-----------------------------------------------------------------------------------------------------------------------
# distortos-doxygen
#-----------------------------------------------------------------------------------------------------------------------

doxygen(INPUT ${CMAKE_CURRENT_SOURCE_DIR}/documentation ${CMAKE_CURRENT_BINARY_DIR}/include
		${CMAKE_CURRENT_SOURCE_DIR}/include
		INCLUDE_PATH ${CMAKE_CURRENT_BINARY_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include)

get_target_property(doxygenCompileOptions distortos COMPILE_OPTIONS)
# for now C++11 standard with GNU extensions is hardcoded here...
execute_process(COMMAND ${CMAKE_COMMAND} -E echo ""
		COMMAND ${CMAKE_CXX_COMPILER} ${doxygenCompileOptions} -std=gnu++11 -E -P -dD -x c++ -
		OUTPUT_VARIABLE preprocessedOutput)
string(REGEX REPLACE "[\r\n]+" ";" preprocessedOutput "${preprocessedOutput}")
foreach(predefinedEntry ${preprocessedOutput})
{'fraction_non_alphanumeric': 0.31801258184619335, 'fraction_numerical': 0.002054178970342791, 'mean_word_length': 2.013346228239845, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1492254', 'n_tokens_mistral': 4404, 'n_tokens_neox': 4036, 'n_words': 676}