@@ -35,6 +35,13 @@ endif()
3535# ##############################################################################
3636option (AL_PYTHON_BINDINGS "Build Python bindings" OFF )
3737
38+ # When ON, the al C++ library is not built here; instead it is located with
39+ # find_package(al-core CONFIG). Enables a two-stage build where libimas-core
40+ # (the C/C++ library) and imas-core (the Python wrapper) are packaged
41+ # separately, e.g. as two conda packages.
42+ option (AL_USE_INSTALLED_CORE
43+ "Link Python bindings against a pre-installed al-core (find_package)" OFF )
44+
3845
3946# Configuration options for shared libraries
4047# ##############################################################################
@@ -127,8 +134,31 @@ if(WIN32)
127134 find_package (dlfcn-win32 CONFIG REQUIRED )
128135endif ()
129136
130- # build AL_CORE library only if backend is enabled
131- if (AL_BACKEND_HDF5 OR AL_BACKEND_MDSPLUS OR AL_BACKEND_UDA OR AL_BACKEND_UDAFAT OR AL_PYTHON_BINDINGS)
137+ # Two-stage build entry: locate a pre-installed al-core instead of building it.
138+ # Exposes target `al-core::al` and an unqualified alias `al` so the python/
139+ # subdirectory and any consumers can link to `al` transparently.
140+ if (AL_USE_INSTALLED_CORE)
141+ find_package (al-core CONFIG )
142+ if (NOT al-core_FOUND)
143+ message (FATAL_ERROR
144+ "AL_USE_INSTALLED_CORE=ON requires an installed al-core that ships "
145+ "al-coreConfig.cmake (introduced in IMAS-Core 5.7.1 or later). "
146+ "find_package(al-core CONFIG) could not locate it.\n "
147+ "Point CMAKE_PREFIX_PATH (or al-core_DIR) at an install prefix that "
148+ "contains lib/cmake/al-core/al-coreConfig.cmake — typically the "
149+ "stage-1 install tree of this branch. Releases built before this "
150+ "change (e.g. IMAS-Core/5.6.0) only ship al-core.pc and will NOT "
151+ "satisfy AL_USE_INSTALLED_CORE; either install stage 1 from source "
152+ "or wait for a release that includes the CMake package config." )
153+ endif ()
154+ if (NOT TARGET al)
155+ add_library (al ALIAS al-core::al )
156+ endif ()
157+ endif ()
158+
159+ # build AL_CORE library only if backend is enabled and we are not reusing an
160+ # already-installed al-core
161+ if ((AL_BACKEND_HDF5 OR AL_BACKEND_MDSPLUS OR AL_BACKEND_UDA OR AL_BACKEND_UDAFAT OR AL_PYTHON_BINDINGS) AND NOT AL_USE_INSTALLED_CORE)
132162
133163# Core dependencies
134164set (Boost_USE_MULTITHREADED FALSE )
@@ -199,14 +229,43 @@ add_dependencies( imas_print_version al )
199229# ##############################################################################
200230
201231# Install al library
232+ include (GNUInstallDirs )
202233install (
203234 TARGETS al
235+ EXPORT al-coreTargets
204236 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
205237 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
206238 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
207239 COMPONENT core
208240)
209241
242+ # Export CMake package config so downstream projects (notably the Python
243+ # wrapper built with AL_USE_INSTALLED_CORE=ON) can `find_package(al-core CONFIG)`.
244+ include (CMakePackageConfigHelpers )
245+ set (_al_core_cmake_dir ${CMAKE_INSTALL_LIBDIR} /cmake/al-core)
246+ install (
247+ EXPORT al-coreTargets
248+ FILE al-coreTargets.cmake
249+ NAMESPACE al-core::
250+ DESTINATION ${_al_core_cmake_dir}
251+ )
252+ configure_package_config_file (
253+ ${CMAKE_CURRENT_SOURCE_DIR } /cmake/al-coreConfig.cmake.in
254+ ${CMAKE_CURRENT_BINARY_DIR } /al-coreConfig.cmake
255+ INSTALL_DESTINATION ${_al_core_cmake_dir}
256+ )
257+ write_basic_package_version_file (
258+ ${CMAKE_CURRENT_BINARY_DIR } /al-coreConfigVersion.cmake
259+ VERSION ${PROJECT_VERSION }
260+ COMPATIBILITY SameMajorVersion
261+ )
262+ install (
263+ FILES
264+ ${CMAKE_CURRENT_BINARY_DIR } /al-coreConfig.cmake
265+ ${CMAKE_CURRENT_BINARY_DIR } /al-coreConfigVersion.cmake
266+ DESTINATION ${_al_core_cmake_dir}
267+ )
268+
210269# TODO: put public heades in a separate directory?
211270install (
212271 FILES ${PUBLIC_HEADER_FILES}
@@ -240,14 +299,15 @@ install(DIRECTORY common TYPE DATA)
240299# Install Dummy
241300install (TARGETS imas_print_version DESTINATION bin)
242301
243- # Scikit-build-core entry point for python bindings
302+ endif () # AL core library: (AL_BACKEND_* OR AL_PYTHON_BINDINGS) AND NOT AL_USE_INSTALLED_CORE
303+
304+ # Scikit-build-core entry point for python bindings — works whether `al` was
305+ # just built here or imported via find_package(al-core).
244306# ##############################################################################
245307if (AL_PYTHON_BINDINGS)
246308 include (skbuild.cmake )
247309endif ()
248310
249- endif () # AL core library (AL_BACKEND_HDF5 OR AL_BACKEND_MDSPLUS OR AL_BACKEND_UDA OR AL_BACKEND_UDAFAT OR AL_PYTHON_BINDINGS)
250-
251311
252312# MDSplus models
253313# ##############################################################################
0 commit comments