1project('pkgconfig-gen', 'c')
2
3# Some CI runners does not have zlib, just skip them as we need some common
4# external dependency.
5cc = meson.get_compiler('c')
6if not cc.find_library('z', required: false).found()
7  error('MESON_SKIP_TEST: zlib missing')
8endif
9
10# First check we have pkg-config >= 0.29
11pkgconfig = find_program('pkg-config', required: false)
12if not pkgconfig.found()
13  error('MESON_SKIP_TEST: pkg-config not found')
14endif
15
16v = run_command(pkgconfig, '--version').stdout().strip()
17if v.version_compare('<0.29')
18  error('MESON_SKIP_TEST: pkg-config version \'' + v + '\' too old')
19endif
20
21pkgg = import('pkgconfig')
22
23lib = shared_library('simple', 'simple.c')
24libver = '1.0'
25h = install_headers('simple.h')
26
27pkgg.generate(
28  libraries : [lib, '-lz'],
29  subdirs : '.',
30  version : libver,
31  name : 'libsimple',
32  filebase : 'simple',
33  description : 'A simple demo library.',
34  requires : 'glib-2.0', # Not really, but only here to test that this works.
35  requires_private : ['gio-2.0', 'gobject-2.0'],
36  libraries_private : [lib, '-lz'],
37)
38
39test('pkgconfig-validation', pkgconfig,
40  args: ['--validate', 'simple'],
41  env: [ 'PKG_CONFIG_PATH=' + meson.current_build_dir() + '/meson-private' ])
42
43# Test that name_prefix='' and name='libfoo' results in '-lfoo'
44lib2 = shared_library('libfoo', 'simple.c',
45  name_prefix : '',
46  version : libver)
47
48pkgg.generate(
49  libraries : lib2,
50  name : 'libfoo',
51  version : libver,
52  description : 'A foo library.',
53  variables : ['foo=bar', 'datadir=${prefix}/data'],
54  extra_cflags : ['-DLIBFOO'],
55)
56
57pkgg.generate(
58  name : 'libhello',
59  description : 'A minimalistic pkgconfig file.',
60  version : libver,
61)
62
63pkgg.generate(
64  name : 'libhello_nolib',
65  description : 'A minimalistic pkgconfig file.',
66  version : libver,
67  dataonly: true,
68  variables : {
69    'foo': 'bar',
70    # prefix is not set by default for dataonly pc files, but it is allowed to
71    # define it manually.
72    'prefix': get_option('prefix'),
73    'escaped_var': 'hello world',
74  },
75  unescaped_variables: {
76    'unescaped_var': 'hello world',
77  }
78)
79
80# Regression test for 2 cases:
81# - link_whole from InternalDependency used to be ignored, but we should still
82#   recurse to add libraries they link to. In this case it must add `-lsimple1`
83#   in generated pc file.
84# - dependencies from InternalDependency used to be ignored. In this it must add
85#   `-lz` in generated pc file.
86simple1 = shared_library('simple1', 'simple.c')
87stat1 = static_library('stat1', 'simple.c', link_with: simple1)
88dep = declare_dependency(link_whole: stat1, dependencies: cc.find_library('z'))
89simple2 = library('simple2', 'simple.c')
90pkgg.generate(simple2, libraries: dep)
91
92# Regression test: as_system() does a deepcopy() of the InternalDependency object
93# which caused `-lsimple3` to be duplicated because generator used to compare
94# Target instances instead of their id.
95simple3 = shared_library('simple3', 'simple.c')
96dep1 = declare_dependency(link_with: simple3)
97dep2 = dep1.as_system()
98pkgg.generate(libraries: [dep1, dep2],
99  name: 'simple3',
100  description: 'desc')
101
102# Regression test: stat2 is both link_with and link_whole, it should not appear
103# in generated pc file.
104stat2 = static_library('stat2', 'simple.c', install: true)
105simple4 = library('simple4', 'simple.c', link_with: stat2)
106simple5 = library('simple5', 'simple5.c', link_with: simple4, link_whole: stat2)
107pkgg.generate(simple5)
108
109# Test passing a linkable CustomTarget and CustomTargetIndex to generator.
110# Do this only with gcc/clang to not have to deal with other compiler command
111# line specificities.
112if cc.get_id() in ['gcc', 'clang']
113  ct = custom_target('ct',
114    input: 'simple.c',
115    output: 'libct.so',
116    command: [cc.cmd_array(), '@INPUT@', '-shared', '-o', '@OUTPUT@'],
117  )
118  pkgg.generate(libraries: ct,
119    name: 'ct',
120    description: 'custom target'
121  )
122  pkgg.generate(libraries: ct[0],
123    name: 'ct0',
124    description: 'custom target index'
125  )
126endif
127