mirror of
https://github.com/eclipse/upm.git
synced 2025-07-02 09:51:14 +03:00
examples: C/C++ examples use transitive dependencies
Updated the examples to comprehend transitive dependencies. This means that each example target will no longer have a giant list of -I includes (the examples at the end of the list had includes for all previous examples, upwards of 200 -I's on the command line). * Created a CMakeLists.txt in the upm/examples directory, moved common functionality to this level. * C/C++ examples now look to the filename for their dependency target name, ie; gas-mq2.cxx adds a dependency to the 'gas' target * Updated a handful of C/C++ example names to reflect this * Example CMake flow - glob the list of files, add targets for any special case examples, then att targets for all the rest Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
118
examples/CMakeLists.txt
Normal file
118
examples/CMakeLists.txt
Normal file
@ -0,0 +1,118 @@
|
||||
# Add an example executable target for the provided source file which depends
|
||||
# on a UPM library target. Strings after the first '-' are ignored.
|
||||
#
|
||||
# Usage:
|
||||
# add_example(source_file TARGETS <target0> <target1> <targetn> SUFFIX <target_suffix>)
|
||||
#
|
||||
# Parameters:
|
||||
# source_file - C/C++ source file. Filename must follow a specific format:
|
||||
# <library>-[additional].c(xx)
|
||||
# library: Name of UPM library required by example
|
||||
# -additional: Any other string used to identify the example
|
||||
# TARGETS - If TARGETS is set, ignore the <library>-[additional] syntax and
|
||||
# use only the provided TARGETS as target name dependencies
|
||||
# SUFFIX - Provide a means to differentiate between C/C++ dependency targets and
|
||||
# example names. Leave empty for CXX examples. Use '-c' for c targets.
|
||||
# SUFFIX Example file Dependency target Example binary
|
||||
# ------ ------------ ----------------- --------------
|
||||
# "-c" foo.c foo-c foo-example-c
|
||||
# foo.cxx foo foo-example-cxx
|
||||
#
|
||||
# Parent scope variable:
|
||||
# example_src_list - Any example target which is successfully created by
|
||||
# add_example will be removed from the parent scope variable
|
||||
# example_src_list (if it exists). This provides a means to handle
|
||||
# special case examples BEFORE handling all files in example_src_list.
|
||||
#
|
||||
# Examples:
|
||||
# # Creates sensorfoo-example-cxx, depends on target: sensorfoo
|
||||
# add_example(sensorfoo.cxx)
|
||||
#
|
||||
# # Creates sensorfoo-example-c, depends on target: sensorfoo-c
|
||||
# add_example(sensorfoo.c SUFFIX "-c")
|
||||
#
|
||||
# # Creates sensorfoo-bar-example-c, depends on target: sensorfoo-c
|
||||
# add_example(sensorfoo-bar.c SUFFIX "-c")
|
||||
#
|
||||
# # Creates testfoobar-cxx, depends on targets: blib, blah
|
||||
# add_example(testfoobar.c TARGETS blib blah)
|
||||
#
|
||||
# Examples:
|
||||
# a110x.c Requires libupmc-a110x
|
||||
# lcm1602-i2c.c Requires libupmc-lcm1602
|
||||
# bmp280-bme280.c Requires libupmc-bmp280
|
||||
# i2clcd-eboled.cxx Requires libupm-i2clcd
|
||||
#
|
||||
#
|
||||
function (add_example example_src)
|
||||
set (oneValueArgs SUFFIX)
|
||||
set (multiValueArgs TARGETS)
|
||||
# Parse function parameters
|
||||
cmake_parse_arguments(add_example "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||
|
||||
# Get the base filename from the full filename
|
||||
# For file /some/path/to/sensorfoo-bar.c, example_name = sensorfoo-bar
|
||||
get_filename_component(example_name ${example_src} NAME_WE)
|
||||
|
||||
# Parse out <library>
|
||||
# Example: sensorfoo-bar.c, library = sensorfoo
|
||||
string (REPLACE "-" ";" split_name ${example_name})
|
||||
list (GET split_name 0 library_name)
|
||||
|
||||
# If TARGETS is not provided, parse dependency target name from example_src
|
||||
if (NOT add_example_TARGETS)
|
||||
# For sensorfoo, lib_target_names = sensorfoo-c
|
||||
set (lib_target_names "${library_name}${add_example_SUFFIX}")
|
||||
else ()
|
||||
set (lib_target_names "${add_example_TARGETS}")
|
||||
endif ()
|
||||
|
||||
# Unfortunately, c++ and c library targets are not named the same. If
|
||||
# no suffix is provided, assume C++ and add a suffix of -cxx to the
|
||||
# example target name
|
||||
# library example target
|
||||
# c: sensorfoo-c sensorfoo-bar-example-c
|
||||
# cxx: sensorfoo sensorfoo-bar-example-cxx
|
||||
if (NOT add_example_SUFFIX)
|
||||
set(add_example_SUFFIX "-cxx")
|
||||
endif ()
|
||||
|
||||
# Create the target name for this example: mylibrary-bar-example-c
|
||||
set (this_target_name "${example_name}-example${add_example_SUFFIX}")
|
||||
|
||||
# If a dependency target does NOT exist, print a warning and skip
|
||||
foreach(_dep_target ${lib_target_names})
|
||||
if (NOT TARGET ${_dep_target})
|
||||
message(STATUS "Missing CMake target (${_dep_target}), skipping example ${example_src}")
|
||||
return()
|
||||
endif ()
|
||||
endforeach ()
|
||||
|
||||
# Create an executable target for this example
|
||||
add_executable (${this_target_name} ${example_src})
|
||||
|
||||
# Add each dependency to the library target
|
||||
foreach(_dep_target ${lib_target_names})
|
||||
target_link_libraries(${this_target_name} ${_dep_target} ${CMAKE_THREAD_LIBS_INIT})
|
||||
endforeach ()
|
||||
|
||||
# Special case...
|
||||
list (REMOVE_ITEM example_src_list ${example_src})
|
||||
set (example_src_list ${example_src_list} PARENT_SCOPE)
|
||||
endfunction (add_example example_src)
|
||||
|
||||
# Add subdirectories if BUILDEXAMPLES=on
|
||||
if(BUILDEXAMPLES)
|
||||
# Add C examples
|
||||
add_subdirectory (c)
|
||||
|
||||
# Add C++ examples?
|
||||
if (BUILDCPP)
|
||||
add_subdirectory (c++)
|
||||
endif ()
|
||||
|
||||
# Add java examples?
|
||||
if(BUILDSWIGJAVA)
|
||||
add_subdirectory (java)
|
||||
endif()
|
||||
endif()
|
Reference in New Issue
Block a user