From 4b8dd68d3c6e9bb03f5f9095f7bf6166ea2a3943 Mon Sep 17 00:00:00 2001 From: Gammasoft Date: Sun, 10 Nov 2024 18:07:18 +0100 Subject: [PATCH] Add target_top_level_statement command --- scripts/cmake/xtd_commands.cmake | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/scripts/cmake/xtd_commands.cmake b/scripts/cmake/xtd_commands.cmake index b442dc6e849..c55fb13c65a 100644 --- a/scripts/cmake/xtd_commands.cmake +++ b/scripts/cmake/xtd_commands.cmake @@ -325,6 +325,57 @@ macro(target_startup ...) set(PROJECT_SOURCES "${PROJECT_SOURCES};${STARTUP_FILE}") endmacro() +## @brief Generates top-level statements with specified source file. +## @param TOP_LEVEL_FILE the file that contains the content of the main entry point. +## @remarks Do not use this method of startup_(...) is present in your files or if you use standard c++ main() method. +## @remarks Call only once by project. +## @remarks This method must be call before target_type(). +## @remarks This method is optional. +## @par Examples +## ```cpp +## cmake_minimum_required(VERSION 3.20) +## +## project(my_project) +## find_package(xtd REQUIRED) +## add_sources(my_project.cpp) +## +## target_top_level_statement(my_project.cpp) +## target_type(GUI_APPLICATION) +## ``` +macro(target_top_level_statement TOP_LEVEL_FILE) + message(VERBOSE "Add top level statement [\"${TOP_LEVEL_FILE}\"]...") + set(STARTUP_FILE properties/startup.cpp) + string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" TOP_LEVEL_FILE ${TOP_LEVEL_FILE}) + file(RELATIVE_PATH STARTUP_OBJECT_RELATIVE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/Properties ${CMAKE_CURRENT_SOURCE_DIR}/${TOP_LEVEL_FILE}) + set(INCLUDE_FILE "#include \"${STARTUP_OBJECT_RELATIVE_PATH}\"\n") + file(WRITE ${STARTUP_FILE} + "#pragma region xtd generated code\n" + "// This code was generated by CMake script.\n" + "//\n" + "// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.\n" + "\n" + "#include \n" + "\n" + "auto main() -> int {\n" + " xtd::startup::safe_run([] {\n" + " using namespace xtd;\n" + " using namespace xtd::collections::generic;\n" + " using namespace xtd::io;\n" + " //using namespace xtd::linq;\n" + " //using namespace xtd::net::http;\n" + " using namespace xtd::threading;\n" + " using namespace xtd::threading::tasks;\n" + " \n" + " ${INCLUDE_FILE}" + " });\n" + "}\n" + "#pragma endregion\n" + ) + #source_group(src\\properties FILES ${STARTUP_FILE}) + auto_source_group(${STARTUP_FILE}) + set(PROJECT_SOURCES "${PROJECT_SOURCES};${STARTUP_FILE}") +endmacro() + ## @brief Specifies the type of application to build. ## @param TYPE Type of application. ## Possible values :