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