Debian Codegraph @

subprojects/meson-docs/docs/markdown/D.md

raw
1 ---
2 title: D
3 short-description: Compiling D sources
4 ...
5
6 # Compiling D applications
7
8 Meson has support for compiling D programs. A minimal `meson.build`
9 file for D looks like this:
10
11 ```meson
12 project('myapp', 'd')
13
14 executable('myapp', 'app.d')
15 ```
16
17 ## [Conditional compilation](https://dlang.org/spec/version.html)
18
19 If you are using the
20 [version()](https://dlang.org/spec/version.html#version-specification)
21 feature for conditional compilation, you can use it using the
22 `d_module_versions` target property:
23
24 ```meson
25 project('myapp', 'd')
26 executable('myapp', 'app.d', d_module_versions: ['Demo', 'FeatureA'])
27 ```
28
29 For debugging, [debug()](https://dlang.org/spec/version.html#debug)
30 conditions are compiled automatically in debug builds, and extra
31 identifiers can be added with the `d_debug` argument:
32
33 ```meson
34 project('myapp', 'd')
35 executable('myapp', 'app.d', d_debug: [3, 'DebugFeatureA'])
36 ```
37
38 ## In `declare_dependency`
39
40 *Since 0.62.0*, when declaring your own dependency using `declare_dependency`,
41 it is possible to add parameters for D specific features, e.g. to propagate
42 conditional compilation versions:
43
44 ```meson
45 my_dep = declare_dependency(
46 # ...
47 d_module_versions: ['LUA_53'],
48 d_import_dirs: include_directories('my_lua_folder'),
49 )
50 ```
51
52 Accepted D specific parameters are `d_module_versions` and `d_import_dirs`
53 (DMD `-J` switch).
54
55 ## Using embedded unittests
56
57 If you are using embedded [unittest
58 functions](https://dlang.org/spec/unittest.html), your source code
59 needs to be compiled twice, once in regular mode, and once with
60 unittests active. This is done by setting the `d_unittest` target
61 property to `true`. Meson will only ever pass the respective
62 compiler's `-unittest` flag, and never have the compiler generate an
63 empty main function. If you need that feature in a portable way,
64 create an empty `main()` function for unittests yourself, since the
65 GNU D compiler does not have this feature.
66
67 This is an example for using D unittests with Meson:
68 ```meson
69 project('myapp_tested', 'd')
70
71 myapp_src = ['app.d', 'alpha.d', 'beta.d']
72 executable('myapp', myapp_src)
73
74 test_exe = executable('myapp_test', myapp_src, d_unittest: true)
75 test('myapptest', test_exe)
76 ```
77
78 # Compiling D libraries and installing them
79
80 Building D libraries is a straightforward process, not different from
81 how C libraries are built in Meson. You should generate a pkg-config
82 file and install it, in order to make other software on the system
83 find the dependency once it is installed.
84
85 This is an example on how to build a D shared library:
86 ```meson
87 project('mylib', 'd', version: '1.2.0')
88
89 project_soversion = 0
90 glib_dep = dependency('glib-2.0')
91
92 my_lib = library('mylib',
93 ['src/mylib/libfunctions.d'],
94 dependencies: [glib_dep],
95 install: true,
96 version: meson.project_version(),
97 soversion: project_soversion,
98 d_module_versions: ['FeatureA', 'featureB', 1]
99 )
100
101 pkgc = import('pkgconfig')
102 pkgc.generate(name: 'mylib',
103 libraries: my_lib,
104 subdirs: 'd/mylib',
105 version: meson.project_version(),
106 description: 'A simple example D library.',
107 d_module_versions: ['FeatureA', 1]
108 )
109 install_subdir('src/mylib/', install_dir: 'include/d/mylib/')
110 ```
111
112 It is important to make the D sources install in a subdirectory in the
113 include path, in this case `/usr/include/d/mylib/mylib`. All D
114 compilers include the `/usr/include/d` directory by default, and if
115 your library would be installed into `/usr/include/d/mylib`, there is
116 a high chance that, when you compile your project again on a machine
117 where you installed it, the compiler will prefer the old installed
118 include over the new version in the source tree, leading to very
119 confusing errors.
120
121 This is an example of how to use the D library we just built and
122 installed in an application:
123 ```meson
124 project('myapp', 'd')
125
126 mylib_dep = dependency('mylib', version: '>= 1.2.0')
127 myapp_src = ['app.d', 'alpha.d', 'beta.d']
128 executable('myapp', myapp_src, dependencies: [mylib_dep])
129 ```
130
131 Please keep in mind that the library and executable would both need to
132 be built with the exact same D compiler and D compiler version. The D
133 ABI is not stable across compilers and their versions, and mixing
134 compilers will lead to problems.
135
136 # Integrating with DUB
137
138 DUB is a fully integrated build system for D, but it is also a way to
139 provide dependencies. Adding dependencies from the [D package
140 registry](https://code.dlang.org/) is pretty straight forward. You can
141 find how to do this in
142 [Dependencies](Dependencies.md#dub). You can also
143 automatically generate a `dub.json` file as explained in
144 [Dlang](Dlang-module.md#generate_dub_file).