Initial work on targets discovery with global var
This commit is contained in:
parent
8d87acda9b
commit
8fbb9a4b23
|
|
@ -0,0 +1,37 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
project(toplevel LANGUAGES C)
|
||||
|
||||
add_subdirectory(libraries)
|
||||
|
||||
set(ALL_MY_PROJECT_TARGETS "" CACHE INTERNAL "Global list")
|
||||
|
||||
# Function to recursively collect all targets
|
||||
function(get_all_project_targets _target_list_var _current_dir)
|
||||
# Get targets defined directly in the current directory
|
||||
get_property(_targets_in_dir DIRECTORY "${_current_dir}" PROPERTY BUILDSYSTEM_TARGETS)
|
||||
message("_targets_in_dir: ${_targets_in_dir}")
|
||||
list(APPEND _ALL_MY_PROJECT_TARGETS ${ALL_MY_PROJECT_TARGETS} ${_targets_in_dir})
|
||||
set(ALL_MY_PROJECT_TARGETS ${_ALL_MY_PROJECT_TARGETS} "" CACHE INTERNAL "")
|
||||
message("ALL_MY_PROJECT_TARGETS: ${ALL_MY_PROJECT_TARGETS}")
|
||||
|
||||
# Get subdirectories of the current directory
|
||||
get_property(_subdirs DIRECTORY "${_current_dir}" PROPERTY SUBDIRECTORIES)
|
||||
foreach(_subdir IN LISTS _subdirs)
|
||||
# Recursively call for each subdirectory
|
||||
message("_subdir: ${_subdir}")
|
||||
get_all_project_targets(${_target_list_var} "${_subdir}")
|
||||
endforeach()
|
||||
endfunction()
|
||||
|
||||
# Call the function from the top-level CMakeLists.txt
|
||||
get_all_project_targets(ALL_MY_PROJECT_TARGETS "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||
|
||||
# Print the list of all targets
|
||||
message("All CMake Targets in this project:")
|
||||
foreach(target_name IN LISTS ALL_MY_PROJECT_TARGETS)
|
||||
message("- ${target_name}")
|
||||
endforeach()
|
||||
|
||||
# Do stuff
|
||||
add_executable(${PROJECT_NAME}-exe main.c)
|
||||
target_link_libraries(${PROJECT_NAME}-exe library_a-lib library_b-lib)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
run: build
|
||||
cmake --build build
|
||||
build:
|
||||
cmake -S . -B $@
|
||||
clean:
|
||||
rm -Rf build
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
add_subdirectory(library_a)
|
||||
add_subdirectory(library_b)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
project(library_a LANGUAGES C)
|
||||
|
||||
add_library(${PROJECT_NAME}-lib OBJECT library_a.c)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
#include <stdio.h>
|
||||
int library_a(void)
|
||||
{
|
||||
printf("%s\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
project(library_b LANGUAGES C)
|
||||
|
||||
add_library(${PROJECT_NAME}-lib OBJECT library_b.c)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int library_b(void)
|
||||
{
|
||||
printf("%s\n", __func__);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue