/ external / libecc / meson.build
meson.build
  1  project('libecc', 'c',
  2      meson_version: '>=1.1.0',
  3      version: run_command('dunamai', 'from', 'git', '--style', 'semver', '--dirty', check: true).stdout().strip(),
  4      default_options: ['c_std=gnu11', 'default_library=static', 'warning_level=3' ],
  5      license: 'BSD-3-Clause OR GPL-2.0-or-later',
  6      license_files: [ 'LICENSE' ],
  7  )
  8  
  9  top_dir = meson.current_source_dir()
 10  
 11  # dunamai is used for version sync with gconf (in the same way poetry-dynamic-versioning does)
 12  pymod = import('python')
 13  py3 = pymod.find_installation('python3', modules: ['dunamai'])
 14  
 15  # about libecc
 16  # below file listings should be moved in successive small 'meson.build' file in each subdir, that
 17  # only contains the file listing, replacing them with a lonely 'subdir('src/xxx') instead.
 18  
 19  ecc_inc = include_directories('include')
 20  subdir('include/libecc')
 21  
 22  # module declaration, each module files listing is declare in its own directory
 23  subdir('src/utils')
 24  subdir('src/nn')
 25  subdir('src/fp')
 26  subdir('src/curves')
 27  subdir('src/hash')
 28  subdir('src/sig')
 29  subdir('src/ecdh')
 30  subdir('src/external_deps')
 31  
 32  # detect if an external rand source implementation is passed
 33  use_external_rand_opt = get_option('use_external_rand')
 34  if use_external_rand_opt
 35    rand_src = files(get_option('with_rand_source'))
 36  else
 37    rand_src = files()
 38  endif
 39  # detect if an external print source implementation is passed
 40  use_external_print_opt = get_option('use_external_print')
 41  if use_external_print_opt
 42    print_src = files(get_option('with_print_source'))
 43  else
 44    print_src = files()
 45  endif
 46  # detect if an external time source implementation is passed
 47  use_external_time_opt = get_option('use_external_time')
 48  if use_external_time_opt
 49    time_src = files(get_option('with_time_source'))
 50  else
 51    time_src = files()
 52  endif
 53  
 54  
 55  # globally used build args, TODO: to make configurable
 56  build_args = [
 57    '-fno-builtin',
 58    '-D_FORTIFY_SOURCE=2',
 59    '-fstack-protector-strong',
 60    '-DUSE_WARN_UNUSED_RET',
 61    '-ffreestanding'
 62  ]
 63  
 64  
 65  # About configurable options, based on makefiles
 66  
 67  # 1. build-relative options first
 68  
 69  # info compilers arguments (such as m32/m64 should be cross-file related.
 70  # just using a different cross-file (event for m32 on m64 arch for e.g. is enough
 71  # to change the behavior
 72  with_wordsize_opt = get_option('with_wordsize')
 73  if with_wordsize_opt == '16'
 74    build_args += '-DWORDSIZE=16'
 75  elif with_wordsize_opt == '32'
 76    build_args += '-DWORDSIZE=32'
 77  elif with_wordsize_opt == '64'
 78    build_args += '-DWORDSIZE=64'
 79  endif
 80  
 81  with_stdlib_opt = get_option('with_stdlib')
 82  if with_stdlib_opt
 83    build_args += '-DWITH_STDLIB'
 84  endif
 85  
 86  with_debug_opt = get_option('with_debug')
 87  if with_debug_opt
 88    build_args += '-DDEBUG'
 89  endif
 90  
 91  use_cryptofuzz_opt = get_option('use_cryptofuzz')
 92  if use_cryptofuzz_opt
 93    build_args += '-DUSE_CRYPTOFUZZ'
 94  endif
 95  
 96  assert_print_opt = get_option('assert_print')
 97  if assert_print_opt
 98    build_args += '-DUSE_ASSERT_PRINT'
 99  endif
100  
101  no_warn_unused_ret_opt = get_option('no_warn_unused_ret')
102  if not no_warn_unused_ret_opt
103    build_args += '-DUSE_WARN_UNUSED_RET'
104  endif
105  
106  # 2. security relative options
107  
108  with_sig_blinding_opt = get_option('with_sig_blinding')
109  if with_sig_blinding_opt
110    build_args += '-DUSE_SIG_BLINDING'
111  endif
112  
113  with_complete_formulas_opt = get_option('with_complete_formulas')
114  if with_complete_formulas_opt
115    build_args += '-DNO_USE_COMPLETE_FORMULAS'
116  endif
117  
118  with_double_add_opt = get_option('with_double_add')
119  if with_double_add_opt == 'true'
120    build_args += '-DUSE_DOUBLE_ADD_ALWAYS'
121  elif with_double_add_opt == 'false'
122    build_args += '-DUSE_MONTY_LADDER'
123  endif
124  
125  with_monty_ladder_opt = get_option('with_monty_ladder')
126  if with_monty_ladder_opt == 'true'
127    build_args += '-DUSE_MONTY_LADDER'
128  elif with_monty_ladder_opt == 'false'
129    build_args += '-DUSE_DOUBLE_ADD_ALWAYS'
130  endif
131  
132  assert(not (with_double_add_opt == 'true' and with_monty_ladder_opt == 'true'), 'with_double_add and with_monty_ladder are incompatible options!')
133  
134  # 3. crypto engines relative options. It is possible to fully disable all
135  # engines and manually select which one must be built
136  
137  with_override_ecc_config_opt = get_option('with_override_ecc_config')
138  if with_override_ecc_config_opt
139    build_args += '-DWITH_LIBECC_CONFIG_OVERRIDE'
140  endif
141  
142  if with_override_ecc_config_opt
143    # Handle the asked curves
144    with_curves_opt = get_option('with_curves')
145    assert(with_curves_opt.length() != 0, 'You have selected a libecc configuration override with no curve: please select at least one proper curve!')
146    foreach curve : with_curves_opt
147      build_args += '-DWITH_CURVE_'+curve.to_upper()
148    endforeach
149    # Handle the asked hashes
150    with_hashes_opt = get_option('with_hashes')
151    assert(with_hashes_opt.length() != 0, 'You have selected a libecc configuration override with no hash: please select at least one proper hash!')
152    foreach hash : with_hashes_opt
153      build_args += '-DWITH_HASH_'+hash.to_upper()
154    endforeach
155    # Handle the asked algorithms
156    with_algs_opt = get_option('with_algs')
157    assert(with_algs_opt.length() != 0, 'You have selected a libecc configuration override with no algorithm: please select at least one proper algorithm!')
158    foreach alg : with_algs_opt
159      build_args += '-DWITH_'+alg.to_upper()
160    endforeach
161  endif
162  
163  # Small stack handling
164  with_small_stack_opt = get_option('with_small_stack')
165  if with_small_stack_opt
166    build_args += '-DUSE_SMALL_STACK'
167  endif
168  assert(not (with_small_stack_opt and with_sig_eddsa25519_opt), 'Small stack and EdDSA are incompatible options!')
169  assert(not (with_small_stack_opt and with_sig_eddsa448_opt), 'Small stack and EdDSA are incompatible options!')
170  assert(not (with_small_stack_opt and with_x25519_opt), 'Small stack and X25519 are incompatible options!')
171  assert(not (with_small_stack_opt and with_x448_opt), 'Small stack and X448 are incompatible options!')
172  
173  
174  # libecc libraries declaration. For each library, the library itself and the
175  # corresponding dependency object (includedir and library to link with) is
176  # also declared
177  #
178  # INFO: defaulting to static lib only (see project declaration).
179  # to build both static and dynamic library, use -Ddefault_library=both option
180  #
181  libarith_lib = library('arith',
182      sources: [
183        fp_mod_src,
184        nn_mod_src,
185        rand_src,
186        print_src,
187        time_src,
188        utils_arith_src,
189      ],
190      include_directories: ecc_inc,
191      install : true,
192      c_args: build_args,
193  )
194  libarith_dep = declare_dependency(
195      link_with: libarith_lib,
196      include_directories: ecc_inc,
197  )
198  
199  libec_lib = library('ecc',
200    sources: [
201      curves_mod_src,
202      utils_ec_src,
203      fp_mod_src,
204      nn_mod_src,
205      rand_src,
206      print_src,
207      time_src,
208      utils_arith_src
209    ],
210    include_directories: ecc_inc,
211    install : true,
212    c_args: build_args,
213  )
214  libec_dep = declare_dependency(
215      link_with: libec_lib,
216      include_directories: ecc_inc,
217  )
218  
219  libsign_lib = library('sign',
220    sources: [
221      hash_mod_src,
222      sig_mod_src,
223      key_mod_src,
224      utils_sign_src,
225      ecdh_mod_src,
226      curves_mod_src,
227      utils_ec_src,
228      fp_mod_src,
229      nn_mod_src,
230      rand_src,
231      print_src,
232      time_src,
233      utils_arith_src
234    ],
235    include_directories: ecc_inc,
236    install : true,
237    c_args: build_args,
238  )
239  libsign_dep = declare_dependency(
240      link_with: libsign_lib,
241      include_directories: ecc_inc,
242  )
243  
244  # in order to build native tools that depends on libsign library, we should
245  # check if the nominal library built has been made native or cross. If
246  # cross, another build must be made natively for native tooling
247  if meson.is_cross_build()
248    native_libsign_lib = library('sign_native',
249      sources: [
250        hash_mod_src,
251        sig_mod_src,
252        key_mod_src,
253        utils_sign_src,
254        ecdh_mod_src,
255        curves_mod_src,
256        utils_ec_src,
257        fp_mod_src,
258        nn_mod_src,
259        rand_src,
260        print_src,
261        time_src,
262        utils_arith_src
263      ],
264      include_directories: ecc_inc,
265      install : false,
266      c_args: build_args,
267      native: true,
268    )
269    native_libsign_dep = declare_dependency(
270      link_with: native_libsign_lib,
271      include_directories: ecc_inc,
272    )
273  else
274      native_libsign_lib = libsign_lib
275      native_libsign_dep = libsign_dep
276  endif
277  
278  # About tests, see src/tests/meson.build. Enabled with -Dwith_tests=true
279  with_tests_opt = get_option('with_tests')
280  if with_tests_opt
281    subdir('src/tests')
282  endif