Simplify gmxapi RPATH handling
[gromacs.git] / src / api / cpp / CMakeLists.txt
blobf22edbbbe84dac8c318a03b264c99798810bb46d
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2018,2019, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
35 # This list file provides the Gromacs::gmxapi cmake module.
37 ##########################
38 # Set up public interface.
40 # The parent (src/api/) CMake scope provides a variable named
41 # GMXAPI_PUBLIC_HEADERS containing the full paths to the public
42 # headers that are being installed. The headers are segregated into a
43 # subdirectory here so that their build-time include directory path
44 # does not expose lower level headers.
46 add_subdirectory(include)
48 # The include directory should be mostly empty so that we can use it internally as
49 # the public interface include directory during build and testing.
50 configure_file(include/version.h.in include/gmxapi/version.h)
52 add_library(gmxapi SHARED
53             context.cpp
54             exceptions.cpp
55             gmxapi.cpp
56             md.cpp
57             mdmodule.cpp
58             mdsignals.cpp
59             session.cpp
60             status.cpp
61             system.cpp
62             version.cpp
63             workflow.cpp
64             tpr.cpp
65             )
66 gmx_target_compile_options(gmxapi)
67 target_compile_definitions(gmxapi PRIVATE HAVE_CONFIG_H)
68 target_include_directories(gmxapi SYSTEM BEFORE PRIVATE ${PROJECT_SOURCE_DIR}/src/external/thread_mpi/include)
70 # Define public interface. Make sure targets linking against `gmxapi` in the build
71 # system don't accidentally have the implementation headers (this directory))
72 # in a default include path.
73 target_include_directories(gmxapi PUBLIC
74                            $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
75                            $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
76                            $<INSTALL_INTERFACE:include>
77                            )
78 # Define implementation interface
79 target_include_directories(gmxapi PRIVATE
80                            ${CMAKE_CURRENT_SOURCE_DIR}
81                            )
83 ###############################
84 # Install the public interface.
86 # If any item begins in a generator expression it must evaluate to a full path,
87 # so we can't just use something like $<TARGET_PROPERTIES:gmxapiPublicHeaders,SOURCES>.
88 # Instead, we use a canonical list defined in the parent scope.
89 install(DIRECTORY include/gmxapi
90         DESTINATION include)
91 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/gmxapi/version.h
92         DESTINATION include/gmxapi)
94 if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
95     # Instruct a linking client to use its RPATH to resolve the libgmxapi location.
96     #
97     # Explicitly specify library "install name" so that the correct loading
98     # instruction is produced in client code. Client code should be able to find the
99     # library relative to the client code RPATH. Without explicitly specifying,
100     # INSTALL_NAME_DIR is inherited from the global CMAKE_INSTALL_NAME_DIR, which is
101     # not appropriate for libgmxapi if it uses an install name relative to the
102     # executable_path or loader_path.
103     set_target_properties(gmxapi PROPERTIES INSTALL_NAME_DIR "@rpath")
104 endif()
106 set_target_properties(gmxapi PROPERTIES
107                       SOVERSION ${GMXAPI_MAJOR}
108                       VERSION ${GMXAPI_RELEASE}
109                       )
111 target_link_libraries(gmxapi PRIVATE libgromacs)
114 ################################################
115 # Install and export gmxapi and Gromacs::gmxapi.
117 # Install the gmxapi target and simultaneously define the export target for
118 # which CMake will create a helper file. Specify the directory for clients to
119 # add to their include path to be able to `#include "gmxapi/some_header.h"`
120 install(TARGETS gmxapi
121         EXPORT gmxapi
122         LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
123         RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
124         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
125         INCLUDES DESTINATION include
126         )
128 # Create the CMake exports file to help other projects build against libgmxapi
129 # as a CMake import target Gromacs::gmxapi.
130 install(EXPORT gmxapi
131         NAMESPACE Gromacs::
132         DESTINATION share/cmake/gmxapi/
133         )
134 add_library(Gromacs::gmxapi ALIAS gmxapi )
136 include(CMakePackageConfigHelpers)
138 configure_package_config_file(
139     cmake/gmxapi-config.cmake.in
140     "${CMAKE_CURRENT_BINARY_DIR}/cmake/gmxapi-config.cmake"
141     INSTALL_DESTINATION share/cmake/gmxapi/
143 write_basic_package_version_file(
144     ${CMAKE_CURRENT_BINARY_DIR}/cmake/gmxapi-config-version.cmake
145     VERSION ${GMXAPI_RELEASE}
146     COMPATIBILITY SameMajorVersion
149 install(
150     FILES
151     ${CMAKE_CURRENT_BINARY_DIR}/cmake/gmxapi-config-version.cmake
152     ${CMAKE_CURRENT_BINARY_DIR}/cmake/gmxapi-config.cmake
153     DESTINATION share/cmake/gmxapi/
156 # We need a CMake target to provide the internal interface(s) of the gmxapi
157 # library implementation.
158 add_library(gmxapi-detail INTERFACE)
159 target_include_directories(gmxapi-detail
160                            INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
162 if(BUILD_TESTING)
163     add_subdirectory(tests)
164     add_subdirectory(workflow/tests)
165 endif()