|
| 1 | +project( |
| 2 | + 'libcppgenerate', |
| 3 | + 'cpp', |
| 4 | + version: '0.2', |
| 5 | + default_options: ['warning_level=3', 'cpp_std=c++11', 'default_library=both'], |
| 6 | +) |
| 7 | + |
| 8 | +# Version of dynamic library |
| 9 | +library_version = '0.0.2' |
| 10 | + |
| 11 | +# Do not install unnecessary things when used as a subproject |
| 12 | +install = not meson.is_subproject() |
| 13 | + |
| 14 | +src = files( |
| 15 | + 'cppgenerate/argument.cpp', |
| 16 | + 'cppgenerate/class.cpp', |
| 17 | + 'cppgenerate/codeblock.cpp', |
| 18 | + 'cppgenerate/constructor.cpp', |
| 19 | + 'cppgenerate/cppgenerateutils.cpp', |
| 20 | + 'cppgenerate/enum.cpp', |
| 21 | + 'cppgenerate/membervariable.cpp', |
| 22 | + 'cppgenerate/method.cpp', |
| 23 | + 'cppgenerate/printer.cpp', |
| 24 | + 'cppgenerate/variable.cpp', |
| 25 | +) |
| 26 | + |
| 27 | +# Set on Windows by default. When enabled, use .lib suffix for static libraries |
| 28 | +# and do not build shared ones in Windows. |
| 29 | +cmake_compatible_behavior = ( |
| 30 | + host_machine.system() in ['windows', 'cygwin'] |
| 31 | + and get_option('cmake-compatible-libraries') |
| 32 | +) |
| 33 | + |
| 34 | +shared_lib = [] |
| 35 | +building_shared_lib = ( |
| 36 | + not cmake_compatible_behavior |
| 37 | + and get_option('default_library') in ['shared', 'both'] |
| 38 | +) |
| 39 | +static_lib = [] |
| 40 | +building_static_lib = ( |
| 41 | + cmake_compatible_behavior |
| 42 | + or get_option('default_library') in ['shared', 'both'] |
| 43 | +) |
| 44 | + |
| 45 | +if (cmake_compatible_behavior) |
| 46 | + if (get_option('default_library') == 'shared') |
| 47 | + warning( |
| 48 | + 'Shared libraries will not be generated on Windows.', |
| 49 | + 'Set -Dcmake-compatible-libraries=false to override', |
| 50 | + ) |
| 51 | + endif |
| 52 | + static_lib = static_library( |
| 53 | + 'cppgenerate', |
| 54 | + src, |
| 55 | + name_suffix: 'lib', |
| 56 | + ) |
| 57 | +elif building_shared_lib and building_static_lib |
| 58 | + if install |
| 59 | + both_libs = both_libraries( |
| 60 | + 'cppgenerate', |
| 61 | + src, |
| 62 | + version: library_version, |
| 63 | + install: true, |
| 64 | + ) |
| 65 | + static_lib = both_libs.get_static_lib() |
| 66 | + shared_lib = both_libs.get_shared_lib() |
| 67 | + else |
| 68 | + # When in a subproject, only install the necessary shared library |
| 69 | + static_lib = static_library( |
| 70 | + 'cppgenerate', |
| 71 | + src, |
| 72 | + ) |
| 73 | + shared_lib = shared_library( |
| 74 | + 'cppgenerate', |
| 75 | + src, |
| 76 | + version: library_version, |
| 77 | + install: true, |
| 78 | + ) |
| 79 | + endif |
| 80 | +elif building_shared_lib |
| 81 | + shared_lib = shared_library( |
| 82 | + 'cppgenerate', |
| 83 | + src, |
| 84 | + version: library_version, |
| 85 | + install: true, |
| 86 | + ) |
| 87 | +elif building_static_lib |
| 88 | + static_lib = static_library( |
| 89 | + 'cppgenerate', |
| 90 | + src, |
| 91 | + install: install, |
| 92 | + ) |
| 93 | +endif |
| 94 | + |
| 95 | +if install |
| 96 | + install_headers( |
| 97 | + 'cppgenerate/accessmodifier.h', |
| 98 | + 'cppgenerate/argument.h', |
| 99 | + 'cppgenerate/class.h', |
| 100 | + 'cppgenerate/codeblock.h', |
| 101 | + 'cppgenerate/constructor.h', |
| 102 | + 'cppgenerate/cppgenerateutils.h', |
| 103 | + 'cppgenerate/enum.h', |
| 104 | + 'cppgenerate/membervariable.h', |
| 105 | + 'cppgenerate/method.h', |
| 106 | + 'cppgenerate/printer.h', |
| 107 | + 'cppgenerate/variable.h', |
| 108 | + subdir: 'cppgenerate', |
| 109 | + ) |
| 110 | +endif |
| 111 | + |
| 112 | +# Configure .pc files |
| 113 | +fs = import('fs') |
| 114 | + |
| 115 | +# Base configuration object used for shared library |
| 116 | +dynamic_lib_cfg = configuration_data() |
| 117 | +dynamic_lib_cfg.set('PROJECT_VERSION', meson.project_version()) |
| 118 | +dynamic_lib_cfg.set('PKG_CONFIG_REQUIRES', '') |
| 119 | +dynamic_lib_cfg.set('CMAKE_INSTALL_PREFIX', get_option('prefix')) |
| 120 | +if fs.is_absolute(get_option('includedir')) |
| 121 | + dynamic_lib_cfg.set('PKG_CONFIG_INCLUDEDIR', get_option('includedir')) |
| 122 | +else |
| 123 | + dynamic_lib_cfg.set( |
| 124 | + 'PKG_CONFIG_INCLUDEDIR', |
| 125 | + '${prefix}' / get_option('includedir'), |
| 126 | + ) |
| 127 | +endif |
| 128 | +if fs.is_absolute(get_option('libdir')) |
| 129 | + dynamic_lib_cfg.set('PKG_CONFIG_LIBDIR', get_option('libdir')) |
| 130 | +else |
| 131 | + dynamic_lib_cfg.set('PKG_CONFIG_LIBDIR', '${prefix}' / get_option('libdir')) |
| 132 | +endif |
| 133 | +dynamic_lib_cfg.set('PKG_CONFIG_LIBS', '-L${libdir} -lcppgenerate') |
| 134 | +dynamic_lib_cfg.set('PKG_CONFIG_CFLAGS', '-I${includedir}') |
| 135 | + |
| 136 | +# Static library configuration object based off of dynamic_lib_cfg |
| 137 | +static_lib_cfg = dynamic_lib_cfg |
| 138 | + |
| 139 | +static_lib_cfg.set('PKG_CONFIG_STATIC_LIBS', '-L${libdir} -l:libcppgenerate.a') |
| 140 | + |
| 141 | +# Configure and install pkg-config files if the relevant library is being built |
| 142 | +if host_machinene.system() not in ['windows', 'cygwin'] |
| 143 | + if install and building_shared_lib |
| 144 | + configure_file( |
| 145 | + input: 'cppgenerate.pc.cmake', |
| 146 | + output: 'cppgenerate.pc', |
| 147 | + configuration: dynamic_lib_cfg, |
| 148 | + format: 'cmake', |
| 149 | + install: true, |
| 150 | + install_dir: get_option('libdir') / 'pkgconfig', |
| 151 | + ) |
| 152 | + endif |
| 153 | + if install and building_static_lib |
| 154 | + configure_file( |
| 155 | + input: 'cppgenerate-static.pc.cmake', |
| 156 | + output: 'cppgenerate-static.pc', |
| 157 | + configuration: static_lib_cfg, |
| 158 | + format: 'cmake', |
| 159 | + install: true, |
| 160 | + install_dir: get_option('libdir') / 'pkgconfig', |
| 161 | + ) |
| 162 | + endif |
| 163 | +endif |
| 164 | + |
| 165 | +# Declare dependency when used as a subproject |
| 166 | +if building_shared_lib |
| 167 | + shared_dep = declare_dependency( |
| 168 | + link_with: shared_lib, |
| 169 | + include_directories: '.', |
| 170 | + ) |
| 171 | + meson.override_dependency('cppgenerate', shared_dep) |
| 172 | +endif |
| 173 | +if building_static_lib |
| 174 | + static_dep = declare_dependency( |
| 175 | + link_with: static_lib, |
| 176 | + include_directories: '.', |
| 177 | + ) |
| 178 | + meson.override_dependency('cppgenerate-static', static_dep) |
| 179 | +endif |
| 180 | + |
| 181 | +# Pick an appropriate dependency object for building tests and examples |
| 182 | +if get_option('default_library') == 'both' |
| 183 | + if meson.version().version_compare('>=1.6.0') |
| 184 | + if ( |
| 185 | + get_option('default_both_libraries') in ['shared', 'auto'] |
| 186 | + and building_shared_lib |
| 187 | + ) |
| 188 | + internal_lib = shared_lib |
| 189 | + else |
| 190 | + internal_lib = static_lib |
| 191 | + endif |
| 192 | + elif building_shared_lib |
| 193 | + internal_lib = shared_lib |
| 194 | + else |
| 195 | + internal_lib = static_lib |
| 196 | + endif |
| 197 | +elif building_shared_lib |
| 198 | + internal_lib = shared_lib |
| 199 | +elif building_static_lib |
| 200 | + internal_lib = static_lib |
| 201 | +endif |
| 202 | + |
| 203 | +internal_dep = declare_dependency( |
| 204 | + link_with: internal_lib, |
| 205 | + include_directories: '.', |
| 206 | +) |
| 207 | + |
| 208 | +if get_option('enable-tests') |
| 209 | + subdir('tests') |
| 210 | +endif |
| 211 | +if get_option('enable-examples') |
| 212 | + subdir('examples') |
| 213 | +endif |
0 commit comments