summaryrefslogtreecommitdiff
path: root/CMake/fl_add_library.cmake
blob: ee795656085f2b99e9d49d718047336d3b8a9e2e (plain)
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
119
120
121
122
123
#
# Macro used by the CMake build system for the Fast Light Tool Kit (FLTK).
# Written by Michael Surette
#
# Copyright 1998-2020 by Bill Spitzak and others.
#
# This library is free software. Distribution and use rights are outlined in
# the file "COPYING" which should have been included with this file.  If this
# file is missing or damaged, see the license at:
#
#     https://www.fltk.org/COPYING.php
#
# Please see the following page on how to report bugs and issues:
#
#     https://www.fltk.org/bugs.php
#

#######################################################################
# FL_ADD_LIBRARY - add a static or shared library to the build
#######################################################################

macro (FL_ADD_LIBRARY LIBNAME LIBTYPE LIBFILES)

  if (${LIBTYPE} STREQUAL "SHARED")
    set (TARGET_NAME ${LIBNAME}_SHARED)
  else ()
    set (TARGET_NAME ${LIBNAME})
  endif (${LIBTYPE} STREQUAL "SHARED")

  if (MSVC)
    set (OUTPUT_NAME_DEBUG   "${LIBNAME}d")
    set (OUTPUT_NAME_RELEASE "${LIBNAME}")
  else ()
    set (OUTPUT_NAME_DEBUG   "${LIBNAME}")
    set (OUTPUT_NAME_RELEASE "${LIBNAME}")
  endif (MSVC)

  add_library(${TARGET_NAME} ${LIBTYPE} ${LIBFILES})

  # Target properties for all libraries

  # Set 'PRIVATE' target compile definitions for the library
  # so they are not inherited by consumers

  target_compile_definitions(${TARGET_NAME}
    PRIVATE "FL_LIBRARY")

  # additional target properties for static libraries

  if (${LIBTYPE} STREQUAL "STATIC")
    set_target_properties(${TARGET_NAME}
      PROPERTIES
      OUTPUT_NAME         ${LIBNAME}
      OUTPUT_NAME_DEBUG   ${OUTPUT_NAME_DEBUG}
      OUTPUT_NAME_RELEASE ${OUTPUT_NAME_RELEASE}
    )
  endif (${LIBTYPE} STREQUAL "STATIC")

  # additional target properties for shared (dynamic) libraries (DLL's)

  if (${LIBTYPE} STREQUAL "SHARED")
    set_target_properties(${TARGET_NAME}
      PROPERTIES
      VERSION             ${FLTK_VERSION}
      SOVERSION           ${FLTK_VERSION_MAJOR}.${FLTK_VERSION_MINOR}
      OUTPUT_NAME         ${LIBNAME}
      OUTPUT_NAME_DEBUG   ${OUTPUT_NAME_DEBUG}
      OUTPUT_NAME_RELEASE ${OUTPUT_NAME_RELEASE}
    )
    # MSVC only:
    if (MSVC)
      set_target_properties(${TARGET_NAME}
        PROPERTIES
        OUTPUT_NAME         lib${LIBNAME}
        OUTPUT_NAME_DEBUG   lib${OUTPUT_NAME_DEBUG}
        OUTPUT_NAME_RELEASE lib${OUTPUT_NAME_RELEASE}
        # PREFIX "lib"    # for MSVC static/shared coexistence *DOES NOT WORK*
      )
    endif (MSVC)
  endif (${LIBTYPE} STREQUAL "SHARED")

  # Debug library output names (optional)
  set (DEBUG_ONAME 0)

  if (DEBUG_ONAME)
    get_target_property (XX_NAME          ${TARGET_NAME} NAME)
    get_target_property (XX_ONAME         ${TARGET_NAME} OUTPUT_NAME)
    get_target_property (XX_ONAME_DEBUG   ${TARGET_NAME} OUTPUT_NAME_DEBUG)
    get_target_property (XX_ONAME_RELEASE ${TARGET_NAME} OUTPUT_NAME_RELEASE)

    fl_debug_var (TARGET_NAME)
    fl_debug_var (XX_NAME)
    fl_debug_var (XX_ONAME)
    fl_debug_var (XX_ONAME_DEBUG)
    fl_debug_var (XX_ONAME_RELEASE)
    message (STATUS "---")
  endif (DEBUG_ONAME)

  if (MSVC)
    if (OPTION_LARGE_FILE)
      set_target_properties(${TARGET_NAME}
        PROPERTIES
        LINK_FLAGS /LARGEADDRESSAWARE
      )
    endif (OPTION_LARGE_FILE)

    if (${LIBTYPE} STREQUAL "SHARED")
      target_compile_definitions(${TARGET_NAME}
        PRIVATE "FL_DLL")
    endif ()
  endif (MSVC)

  install (TARGETS ${TARGET_NAME}
    EXPORT FLTK-Targets
    RUNTIME DESTINATION ${FLTK_BINDIR}
    LIBRARY DESTINATION ${FLTK_LIBDIR}
    ARCHIVE DESTINATION ${FLTK_LIBDIR}
  )

  list (APPEND FLTK_LIBRARIES "${TARGET_NAME}")
  set (FLTK_LIBRARIES ${FLTK_LIBRARIES} PARENT_SCOPE)

endmacro (FL_ADD_LIBRARY LIBNAME LIBTYPE LIBFILES)