Linking Ada objects into a C++ shared library

You cannot mix custom commands and libraries. add_library() is only able to handle sources for supported languages. So, somefile.adb is simply ignored.

To work-around this, manage all ada source files compilation by custom commands (what you already did) and a custom target (see add_custom_target()). And add explicitly the dependency between targets (add_dependencies()) to ensure compilation.

And to finish, compute the list of ada objects and add them explicitly to the link step of you library.

Something like that:

set (ADA_OUTPUTS)
foreach (...)
   ...
  add_custom_command(OUTPUT ${WORK_DIR}/${target}.o ...)
  list(APPEND ADA_OUTPUTS ${WORK_DIR}/${target}.o)
endforeach()
add_custom_target(ada_objects
   DEPENDS ${ADA_OUTPUTS}
   ...
)

add_library(main SHARED ...)
add_dependencies(main ada_objects)
target_link_libraries(main PRIVATE ${ADA_OUTPUTS})

And FYI, I am currently evaluating the integration of Ada language support in CMake (gnat specifically). I think it is feasible but many changes are required in the code of CMake.
For now, I am just able to pass the initialization step and compile a simple ada executable.

For the fun:

cmake_minimum_required(VERSION 3.19)

project(Tests LANGUAGES Ada)

set(CMAKE_Ada_STANDARD 2012)

add_executable(ada_test hello.adb)
set_property(TARGET ada_test PROPERTY MAIN_NAME hello)

And lauching cmake gives:

-- The Ada compiler identification is GNU 2020
-- Checking whether Ada compiler has -isysroot
-- Checking whether Ada compiler has -isysroot - yes
-- Checking whether Ada compiler supports OSX deployment target flag
-- Checking whether Ada compiler supports OSX deployment target flag - yes
-- Detecting Ada compiler ABI info
-- Detecting Ada compiler ABI info - done
-- Check for working Ada compiler: /usr/local/opt/GNAT/2020/bin/gnat - skipped
-- Detecting Ada compile features
-- Detecting Ada compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: .../build

and the compilation:

[ 50%] Building Ada object CMakeFiles/ada_test.dir/hello.o
/usr/local/opt/GNAT/2020/bin/gnat make -c -u -D CMakeFiles/ada_test.dir .../hello.adb -cargs   -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -gnat2012
gcc -c -I/Users/marc/tests/cmake/ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -gnat2012 -I- -o .../build/CMakeFiles/ada_test.dir/hello.o .../hello.adb
[100%] Linking Ada executable ada_test
/usr/local/opt/GNAT/2020/bin/gnat make -b -l -o ada_test hello -aOCMakeFiles/ada_test.dir -cargs  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -largs
gnatbind -aOCMakeFiles/ada_test.dir -x .../build/CMakeFiles/ada_test.dir/hello.ali
gnatlink .../build/CMakeFiles/ada_test.dir/hello.ali -o ada_test
[100%] Built target ada_test

1 Like