about summary refs log tree commit diff
path: root/third_party/nix/meson.build
blob: bd5cd8ce7e969372177c2787dfabd6174933811a (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
project(
    'nix',
    'cpp',

    default_options : [
        'cpp_std=c++17',
        'warning_level=3',
        'optimization=3',
        'debug=true'
    ],
    version : run_command('cat', './.version').stdout().strip(),
    license : 'MIT'
)

# init compiler
cpp = meson.get_compiler('cpp')

add_project_arguments(get_option('cxxflags'), language : 'cpp')
add_project_link_arguments(get_option('ldflags'), language: 'cpp')

cmake = import('cmake')
pkg = import('pkgconfig')

config_h = configuration_data()

config_h.set(
    'HAVE_CXX17', 1,
    description : 'Define if the compiler supports basic C++17 syntax')

package_name = meson.project_name()
config_h.set_quoted(
    'PACKAGE_NAME', package_name,
    description : 'Define to the full name of this package.'
    )

package_tarname = meson.project_name()
config_h.set_quoted(
    'PACKAGE_TARNAME', package_tarname,
    description : 'Define to the one symbol short name of this package.')

package_version = meson.project_version()
config_h.set_quoted(
    'PACKAGE_VERSION', package_version,
    description : 'Define to the version of this package.')

package_string = '@0@ @1@'.format(package_name, package_version)
config_h.set_quoted(
    'PACKAGE_STRING', package_string,
    description : 'Define to the full name and version of this package.')

package_url = 'https://nixos.org/nix/'
config_h.set_quoted(
    'PACKAGE_URL', package_url,
    description : 'Define to the home page for this package.')

package_bug_url = 'https://github.com/nixos/nix/issues'
config_h.set_quoted(
    'PACKAGE_BUGREPORT', package_bug_url,
    description : 'Define to the address where bug reports for this package should be sent.')

# env

# set install directories
#-------------------------------------------------
prefix = get_option('prefix')
libdir = join_paths(prefix, get_option('libdir'))
bindir = join_paths(prefix, get_option('bindir'))
datadir = join_paths(prefix, get_option('datadir'))
sysconfdir = join_paths(prefix, get_option('sysconfdir'))
libexecdir = join_paths(prefix, get_option('libexecdir'))
mandir = join_paths(prefix, get_option('mandir'))
includedir = join_paths(prefix, get_option('includedir'))

# set nix directories

# State should be stored in /nix/var, unless the user overrides it explicitly.
if get_option('normal_var')
    localstatedir = '/nix/var'
else
    localstatedir =  join_paths(prefix, get_option('localstatedir'))
endif

nixstoredir = get_option('nixstoredir')

profiledir =  join_paths(sysconfdir, 'profile.d')

# Construct a Nix system name (like "i686-linux").
#-------------------------------------------------
machine_name = host_machine.cpu()
sys_name = host_machine.system().to_lower()

cpu_archs = ['x86_64', 'armv6', 'armv7', '']

foreach cpu : cpu_archs
    if (host_machine.cpu().contains(cpu))
        if cpu.contains('armv')
            machine_name = cpu + '1'
        else
            machine_name = cpu
        endif
        break
    endif
endforeach

system= '"' + machine_name + '-' + sys_name + '"'
message('system name: ' + system)
config_h.set(
    'SYSTEM', system,
    description : 'platform identifier (`cpu-os`)')




# checking headers
#============================================================================

if (cpp.has_header('sys/stat.h'))
    config_h.set(
        'HAVE_SYS_STAT_H', 1,
        description : 'Define to 1 if you have the <sys/stat.h> header file.')
endif

if (cpp.has_header('sys/types.h'))
    config_h.set(
        'HAVE_SYS_TYPES_H', 1,
        description : 'Define to 1 if you have the <sys/types.h> header file.')
endif

if (cpp.has_header('sys/dir.h'))
    config_h.set(
        'HAVE_DIR_H', 1,
        description : 'Define to 1 if you have the <sys/dir.h> header file, and it defines `DIR`')
endif

if (cpp.has_header('sys/ndir.h'))
    config_h.set(
        'HAVE_NDIR_H', 1,
        description : 'Define to 1 if you have the <sys/ndir.h> header file, and it defines `DIR`')
endif

has_dirent_h = cpp.has_header('dirent.h')
if (has_dirent_h)
    config_h.set(
        'HAVE_DIRENT_H', 1,
        description : 'Define to 1 if you have the <dirent.h> header file, and it defines `DIR`')
endif

if (cpp.has_header('locale.h'))
    config_h.set(
        'HAVE_LOCALE', 1,
        description : 'Define to 1 if you have the <locale.h> header file.')
endif

if (cpp.has_header('unistd.h'))
    config_h.set(
        'HAVE_UNISTD_H', 1,
        description: 'Define to 1 if you have the <unistd.h> header file.')
endif

if (cpp.has_header('stdint.h'))
    config_h.set(
        'HAVE_STDINT_H', 1,
        description: 'Define to 1 if you have the <stdint.h> header file.')
endif

if (cpp.has_header('stdlib.h'))
    config_h.set(
        'HAVE_STDLIB_H', 1,
        description: 'Define to 1 if you have the <stdlib.h> header file.')
endif

if (cpp.has_header('strings.h'))
    config_h.set(
        'HAVE_STRINGS_H', 1,
        description: 'Define to 1 if you have the <strings.h> header file.')
endif

if (cpp.has_header('string.h'))
    config_h.set(
        'HAVE_STRING_H', 1,
        description: 'Define to 1 if you have the <strings.h> header file.')
endif

if (cpp.has_header('bzlib.h'))
    config_h.set(
        'HAVE_BZLIB_H', 1,
        description : 'Define to 1 if you have the <bzlib.h> header file.')
endif

if (cpp.has_header('inttypes.h'))
    config_h.set(
        'HAVE_INTTYPES_H', 1,
        description : 'Define to 1 if you have the <inttypes.h> header file.')
endif

if (cpp.has_header('memory.h'))
    config_h.set(
        'HAVE_MEMORY_H', 1,
        description : 'Define to 1 if you have the <memory.h> header file.')
endif

if (cpp.has_header('editline.h'))
    config_h.set(
        'HAVE_EDITLINE_H', 1,
        description : 'Define to 1 if you have the <editline.h> header file.')
else
    error('Nix requires editline.h; however the header was not found.')
endif




# checking functions
#============================================================================


if (cpp.has_function('lutimes'))
    config_h.set(
        'HAVE_LUTIMES', 1,
        description : 'Define to 1 if you have the `lutimes` function.')
endif

if (cpp.has_function('lchown'))
    config_h.set(
        'HAVE_LCHOWN', 1,
        description : 'Define to 1 if you have the `lchown` function.')
endif

if (cpp.has_function('pipe2'))
    config_h.set(
        'HAVE_PIPE2', 1,
        description : 'Define to 1 if you have the `pipe2` function.')
endif

if (cpp.has_function('posix_fallocate'))
    config_h.set(
        'HAVE_POSIX_FALLOCATE', 1,
        description : 'Define to 1 if you have the `posix_fallocate` function.')
endif

if (cpp.has_function('setresuid'))
    config_h.set(
        'HAVE_SETRESUID', 1,
        description : 'Define to 1 if you have the `setresuid` function.')
endif

if (cpp.has_function('setreuid'))
    config_h.set(
        'HAVE_SETREUID', 1,
        description : 'Define to 1 if you have the `setreuid` function.')
endif

if (cpp.has_function('statvfs'))
    config_h.set(
        'HAVE_STATVFS', 1,
        description : 'Define to 1 if you have the `statvfs` function.')
endif

if (cpp.has_function('strsignal'))
    config_h.set(
        'HAVE_STRSIGNAL', 1,
        description : 'Define to 1 if you have the `strsignal` function.')
endif

if (cpp.has_function('sysconf'))
    config_h.set(
        'HAVE_SYSCONF', 1,
        description : 'Define to 1 if you have the `sysconf` function.')
endif

pubsetbuff_c = '''
    #include <iostream>
    using namespace std;
    static char buf[1024];
    void func() {
    cerr.rdbuf()->pubsetbuf(buf, sizeof(buf));
    }'''

if meson.get_compiler('cpp').compiles(
    pubsetbuff_c,
    name : 'pubsetbuf'
  )
    config_h.set(
        'HAVE_PUBSETBUF', 1,
        description : 'Define to 1 if you have the `pubsetbuf` function.')
endif



# checking data types
#============================================================================


dirent_h_prefix = '''
  #include <sys/types.h>
  #include <dirent.h>
'''

if has_dirent_h and meson.get_compiler('cpp').has_member('struct dirent', 'd_type', prefix: dirent_h_prefix)
  config_h.set('HAVE_STRUCT_DIRENT_D_TYPE', 1)
endif

# required dependancies
#============================================================================


# look for required programs
#--------------------------------------------------
cat = find_program('cat', required : true)
ln = find_program('ln', required : true)
cp = find_program('cp', required : true)
rm = find_program('rm', required : true)
bash = find_program('bash', required : true)
echo = find_program('echo', required : true)
patch = find_program('patch', required : true)
xmllint = find_program('xmllint', required : true)
flex = find_program('flex', required : true)
bison = find_program('bison', required : true)
sed = find_program('sed', required : true)
tar = find_program('tar', required : true)
bzip2 = find_program('bzip2', required : true)
gzip = find_program('gzip', required : true)
xz = find_program('xz', required : true)
dot = find_program('dot', required : false)
lsof = find_program('lsof', required : false)
tr = find_program('tr', required : true)
coreutils = run_command('dirname', cat.path()).stdout().strip()


# Check whether the store optimiser can optimise symlinks.
#-------------------------------------------------
gen_header = '''
ln -s bla tmp_link
if ln tmp_link tmp_link2 2> /dev/null; then
    echo 1
else
    echo 0
fi
'''

run_command('sh', '-c', 'rm tmp_link*')
can_link_symlink = run_command('sh', '-c', gen_header).stdout().strip()
if can_link_symlink.to_int() == 1
    run_command('sh', '-c', 'rm tmp_link*')
endif

config_h.set('CAN_LINK_SYMLINK', can_link_symlink,
description : 'Whether link() works on symlinks')


# Look for boost, a required dependency.
#--------------------------------------------------
boost_dep = declare_dependency(
    dependencies : [
        cpp.find_library('boost_system'),
        cpp.find_library('boost_context'),
        cpp.find_library('boost_thread')],
    link_args :  get_option('boost_link_args'))

if (boost_dep.found())
    config_h.set('HAVE_BOOST', 1, description : 'Define if the Boost library is available.')
endif


# Look for liblzma, a required dependency.
#--------------------------------------------------
liblzma_dep = declare_dependency(
    dependencies: dependency('liblzma'),
    link_args :  get_option('lzma_link_args'))


# Look for libbrotli{enc,dec}.
#--------------------------------------------------
libbrotli_dep = declare_dependency(
    dependencies: [
        dependency('libbrotlienc'),
        dependency('libbrotlidec')],
    link_args :  get_option('brotli_link_args'))


# Look for OpenSSL, a required dependency.
#--------------------------------------------------
openssl_dep = declare_dependency(
    dependencies: cpp.find_library('ssl'),
    link_args :  get_option('openssl_link_args'))


# Look for SQLite, a required dependency.
#--------------------------------------------------
sqlite3_dep = declare_dependency(
    dependencies : dependency('sqlite3', version : '>= 3.6.19'),
    link_args : get_option('sqlite3_link_args'))


# Look for libcurl, a required dependency.
#--------------------------------------------------
libcurl_dep = declare_dependency(
    dependencies : dependency('libcurl'),
    link_args :  get_option('curl_link_args'))


# Look for pthread, a required dependency.
#--------------------------------------------------
pthread_dep = declare_dependency(
    dependencies : dependency('threads'),
    link_args :  get_option('pthread_link_args'))


# Look for libdl, a required dependency.
#--------------------------------------------------
libdl_dep = declare_dependency(
    dependencies : cpp.find_library('dl'),
    link_args :  get_option('dl_link_args'))


# Look for libbz2, a required dependency.
#--------------------------------------------------
libbz2_dep = declare_dependency(
    dependencies : cpp.find_library('bz2'),
    link_args :  get_option('bz2_link_args'))


# Look for editline, a required dependency.
#--------------------------------------------------
# NOTE: The the libeditline.pc file was added only in libeditline >= 1.15.2, see
# https://github.com/troglobit/editline/commit/0a8f2ef4203c3a4a4726b9dd1336869cd0da8607,
# but e.g. Ubuntu 16.04 has an older version, so we fall back to searching for
# editline.h when the pkg-config approach fails.

editline_dep = declare_dependency(
    dependencies : cpp.find_library('editline'),
    link_args :  get_option('editline_link_args'))

if not (
    cpp.has_function(
        'read_history',
        prefix : '#include <stdio.h>\n#include "editline.h"',
        args : '-lreadline'))
    warning('Nix requires libeditline; However, required functions do not work. Maybe ' +\
    'it is too old? >= 1.14 is required.')
endif




# Optional dependancies
#============================================================================


# Look for libsodium, an optional dependency.
#--------------------------------------------------
libsodium_lib = cpp.find_library('sodium', required: get_option('with_libsodium'))
if (libsodium_lib.found())
    libsodium_dep = declare_dependency(
        dependencies : libsodium_lib,
        link_args :  get_option('sodium_link_args'))
    config_h.set('HAVE_SODIUM', 1, description : 'Whether to use libsodium for cryptography.')
else
    libsodium_dep = dependency('', required: false)
endif


# Look for Boehm garbage collector, a required dependency.
# TODO(tazjin): Remove option to disable GC
gc_dep = declare_dependency(
    dependencies : dependency('bdw-gc'),
    link_args :  get_option('gc_link_args'))
config_h.set(
    'HAVE_BOEHMGC', 1,
    description : 'Whether to use the Boehm garbage collector.')

# Look for aws-cpp-sdk-s3.
#--------------------------------------------------
if (get_option('with_s3'))
  enable_s3 = meson.get_compiler('cpp').check_header('aws/s3/S3Client.h')

  aws_version = meson.get_compiler('cpp').get_define(
    'AWS_SDK_VERSION_STRING',
    prefix : '#include <aws/core/VersionConfig.h>'
  ).strip('"').split('.')

  conf_data.set('ENABLE_S3', 1, description : 'Whether to enable S3 support via aws-sdk-cpp.')
  conf_data.set('AWS_VERSION_MAJOR', aws_version[0], description : 'Major version of aws-sdk-cpp.')
  conf_data.set('AWS_VERSION_MINOR', aws_version[1], description : 'Minor version of aws-sdk-cpp.')
endif




# OS Specific checks
#============================================================================
# Look for libsecppomp, required for Linux sandboxing.
if sys_name.contains('linux')
    libseccomp_dep = dependency(
        'libseccomp',
        version : '>= 2.3.1',
        not_found_message : 'Nix requires libseccomp on a linux host system')
    config_h.set(
        'HAVE_SECCOMP', 1,
        description : 'Whether seccomp is available and should be used for sandboxing.')
else
    libseccomp_dep = dependency('', required: false)
endif

if (sys_name.contains('freebsd'))
    add_project_arguments('-D_GNU_SOURCE', language : 'cpp')
    config_h.set('_GNU_SOURCE', 1)
endif

if (sys_name.contains('sunos'))
    # Solaris requires -lsocket -lnsl for network functions
endif




# build
#============================================================================

conf = configure_file(
    output : 'config.h',
    configuration : config_h)

install_headers(
    conf,
    install_dir : join_paths(includedir, 'nix'))

add_project_arguments('-include', 'config.h', language : 'cpp')
src_inc = [include_directories('.', 'src')]

project_dirs = [
    'src',
    'scripts',
    'corepkgs',
    'misc',
    'doc',
    'tests'
]


foreach dir : project_dirs
    subdir(dir)
endforeach