File: MakeLib.cmake

package info (click to toggle)
bornagain 23.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,948 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (118 lines) | stat: -rw-r--r-- 4,077 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
##  ************************************************************************************************
##
##  BornAgain: simulate and fit reflection and scattering
##
##! @file      cmake/BornAgain/MakeLib.cmake
##! @brief     Define function MakeLib, for configuring one component library.
##!
##! @homepage  http://www.bornagainproject.org
##! @license   GNU General Public License v3 or higher (see COPYING)
##! @copyright Forschungszentrum Jülich GmbH 2024
##! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
##
##  ************************************************************************************************

# Add a BornAgain library to the Python wheel
# NOTE: Target 'ba_wheel' must be already defined.
# NOTE: only used by function MakeLib.

function(add_library_to_wheel lib)

    add_dependencies(ba_wheel ${lib})
    # eg., 'libBornAgain.so'
    get_target_property(libfilename ${lib} _BASEFILENAME)

    # copy the shared library and its Python interface to the Python wheel folder
    set(_dst ${BA_PY_LIBRARY_OUTPUT_DIR}/)

    add_custom_command(TARGET ${lib}
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:${lib}>" ${_dst}/${libfilename}
        COMMENT "Add library ${lib} to the Python wheel..."
    )

    get_target_property(lib_LIBRARY_PY ${lib} _LIBRARY_PY)
    string(STRIP ${lib_LIBRARY_PY} lib_LIBRARY_PY)
    if(lib_LIBRARY_PY)
        add_custom_command(TARGET ${lib}
            POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy "${lib_LIBRARY_PY}" ${_dst}
            COMMENT "Add Python wrapper-script of library ${lib} to the Python wheel..."
        )
    endif()

endfunction()


# Configure one component library.

function(MakeLib lib swigtmpdir source_files include_files)

    string(STRIP "${swigtmpdir}" swigtmpdir)

    set_target_properties(${lib} PROPERTIES
        SOURCES "${source_files}"
        PREFIX ${libprefix}
        SUFFIX ${libsuffix}
        VERSION ${PROJECT_VERSION}
        SOVERSION ${PROJECT_VERSION}
        OUTPUT_NAME ${lib}
        # eg., libBornAgainBase.so
        _BASEFILENAME ${libprefix}${lib}${libsuffix})

    get_target_property(lib_dir ${lib} LIBRARY_OUTPUT_DIRECTORY)
    get_target_property(lib_name ${lib} _BASEFILENAME)
    set(BornAgain_LIBRARIES "$CACHE{BornAgain_LIBRARIES};${lib_dir}/${lib_name}"
        CACHE INTERNAL "BornAgain libraries")


    # Set runtime-location of library dependencies
    # See our deployment paper (Nejati et al 2024) for explanation.
    if(LINUX)
        set(link_flags "-Wl,--disable-new-dtags,-rpath,\$ORIGIN:\$ORIGIN/../extra")
        set_target_properties(${lib} PROPERTIES LINK_FLAGS ${link_flags})
    elseif(APPLE)
        target_link_options(${lib} PRIVATE
            "-Wl,-rpath,@loader_path,-rpath,@loader_path/extra")
    endif()


    if(BORNAGAIN_PYTHON)
        target_compile_definitions(${lib} PRIVATE -DBORNAGAIN_PYTHON)

        # SWIG-produced interface
        if(swigtmpdir)
            SwigLib(${lib} ${swigtmpdir} "${include_files}")
        endif()

        # add library to the Python wheel
        if (BA_PY_PACK)
            add_library_to_wheel(${lib})
        endif(BA_PY_PACK)

        if(WIN32)
            # Under Windows, libraries must be in the same folder as that of the executable
            add_custom_command(
                TARGET ${lib}
                POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_BINARY_DIR}/bin/${libprefix}${lib}${libsuffix}
                ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${libprefix}${lib}${libsuffix}
            )
        endif()

    endif() # BORNAGAIN_PYTHON

    # installation
    install(TARGETS ${lib}
        LIBRARY DESTINATION ${destination_lib} COMPONENT Libraries
        RUNTIME DESTINATION ${destination_lib} COMPONENT Libraries)

    if(BA_CPP_API)
        foreach(file ${include_files})
            get_filename_component(dir ${file} DIRECTORY)
            install(FILES ${file} DESTINATION ${destination_include}/${name}/${dir})
        endforeach()
    endif()

endfunction()