Fix .pc file.
[libdcp.git] / wscript
1 import subprocess
2 import os
3
4 APPNAME = 'libdcp'
5 VERSION = '1.00.0devel'
6 API_VERSION = '-1.0'
7
8 def options(opt):
9     opt.load('compiler_cxx')
10     opt.add_option('--target-windows', action='store_true', default=False, help='set up to do a cross-compile to Windows')
11     opt.add_option('--target-osx', action='store_true', default=False, help='set up to build on OS X')
12     opt.add_option('--enable-debug', action='store_true', default=False, help='build with debugging information and without optimisation')
13     opt.add_option('--static', action='store_true', default=False, help='build libdcp and in-tree dependencies statically, and link statically to openjpeg and cxml')
14
15 def configure(conf):
16     conf.load('compiler_cxx')
17     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
18     conf.env.append_value('CXXFLAGS', ['-DLIBDCP_VERSION="%s"' % VERSION])
19
20     conf.env.TARGET_WINDOWS = conf.options.target_windows
21     conf.env.TARGET_OSX = conf.options.target_osx
22     conf.env.ENABLE_DEBUG = conf.options.enable_debug
23     conf.env.STATIC = conf.options.static
24     conf.env.API_VERSION = API_VERSION
25
26     if conf.options.target_windows:
27         conf.env.append_value('CXXFLAGS', '-DLIBDCP_WINDOWS')
28     else:
29         conf.env.append_value('CXXFLAGS', '-DLIBDCP_POSIX')
30
31     if not conf.options.target_osx:
32         conf.env.append_value('CXXFLAGS', ['-Wno-unused-result', '-Wno-unused-parameter'])
33
34     conf.check_cfg(package='openssl', args='--cflags --libs', uselib_store='OPENSSL', mandatory=True)
35     conf.check_cfg(package='libxml++-2.6', args='--cflags --libs', uselib_store='LIBXML++', mandatory=True)
36     conf.check_cfg(package='xmlsec1', args='--cflags --libs', uselib_store='XMLSEC1', mandatory=True)
37     # Remove erroneous escaping of quotes from xmlsec1 defines
38     conf.env.DEFINES_XMLSEC1 = [f.replace('\\', '') for f in conf.env.DEFINES_XMLSEC1]
39     conf.check_cfg(package='', path='Magick++-config', args='--cppflags --cxxflags --libs', uselib_store='MAGICK', mandatory=False)
40     conf.check_cfg(package='sndfile', args='--cflags --libs', uselib_store='SNDFILE', mandatory=False)
41
42     if conf.options.static:
43         conf.check_cc(fragment="""
44                      #include <stdio.h>\n
45                      #include <openjpeg.h>\n
46                      int main () {\n
47                      void* p = (void *) opj_image_create;\n
48                      return 0;\n
49                      }
50                      """,
51                        msg='Checking for library openjpeg', stlib='openjpeg', uselib_store='OPENJPEG', mandatory=True)
52         
53         conf.env.HAVE_CXML = 1
54         conf.env.STLIB_CXML = ['cxml']
55     else:
56         conf.check_cfg(package='libopenjpeg', args='--cflags --libs', uselib_store='OPENJPEG', mandatory=True)
57         conf.check_cfg(package='libcxml', atleast_version='0.08', args='--cflags --libs', uselib_store='CXML', mandatory=True)
58
59     if conf.options.target_windows:
60         boost_lib_suffix = '-mt'
61     else:
62         boost_lib_suffix = ''
63
64     if conf.options.enable_debug:
65         conf.env.append_value('CXXFLAGS', '-g')
66     else:
67         # Somewhat experimental use of -O2 rather than -O3 to see if
68         # Windows builds are any more reliable
69         conf.env.append_value('CXXFLAGS', '-O2')
70
71     conf.check_cxx(fragment="""
72                             #include <boost/version.hpp>\n
73                             #if BOOST_VERSION < 104500\n
74                             #error boost too old\n
75                             #endif\n
76                             int main(void) { return 0; }\n
77                             """,
78                    mandatory=True,
79                    msg='Checking for boost library >= 1.45',
80                    okmsg='yes',
81                    errmsg='too old\nPlease install boost version 1.45 or higher.')
82
83     conf.check_cxx(fragment="""
84                             #include <boost/filesystem.hpp>\n
85                             int main() { boost::filesystem::copy_file ("a", "b"); }\n
86                             """,
87                    msg='Checking for boost filesystem library',
88                    libpath='/usr/local/lib',
89                    lib=['boost_filesystem%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
90                    uselib_store='BOOST_FILESYSTEM')
91
92     conf.check_cxx(fragment="""
93                             #include <boost/signals2.hpp>\n
94                             int main() { boost::signals2::signal<void (int)> x; }\n
95                             """,
96                    msg='Checking for boost signals2 library',
97                    uselib_store='BOOST_SIGNALS2')
98
99     conf.check_cxx(fragment="""
100                             #include <boost/date_time.hpp>\n
101                             int main() { boost::gregorian::day_clock::local_day(); }\n
102                             """,
103                    msg='Checking for boost datetime library',
104                    libpath='/usr/local/lib',
105                    lib=['boost_date_time%s' % boost_lib_suffix, 'boost_system%s' % boost_lib_suffix],
106                    uselib_store='BOOST_DATETIME')
107
108     conf.recurse('test')
109     conf.recurse('asdcplib')
110
111 def build(bld):
112     create_version_cc(bld, VERSION)
113
114     if bld.env.TARGET_WINDOWS:
115         boost_lib_suffix = '-mt'
116     else:
117         boost_lib_suffix = ''
118
119     bld(source='libdcp%s.pc.in' % bld.env.API_VERSION,
120         version=VERSION,
121         includedir='%s/include/libdcp%s' % (bld.env.PREFIX, bld.env.API_VERSION),
122         libs="-L${libdir} -ldcp%s -lasdcp-libdcp%s -lkumu-libdcp%s -lcxml -lboost_system%s" % (API_VERSION, bld.env.API_VERSION, bld.env.API_VERSION, boost_lib_suffix),
123         install_path='${LIBDIR}/pkgconfig')
124
125     bld.recurse('src')
126     bld.recurse('tools')
127     bld.recurse('test')
128     bld.recurse('asdcplib')
129     bld.recurse('examples')
130
131     bld.add_post_fun(post)
132
133 def dist(ctx):
134     ctx.excl = 'TODO core *~ .git build .waf* .lock* doc/*~ src/*~ test/ref/*~ __pycache__ GPATH GRTAGS GSYMS GTAGS'
135
136 def create_version_cc(bld, version):
137     if os.path.exists('.git'):
138         cmd = "LANG= git log --abbrev HEAD^..HEAD ."
139         output = subprocess.Popen(cmd, shell=True, stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0].splitlines()
140         o = output[0].decode('utf-8')
141         commit = o.replace ("commit ", "")[0:10]
142     else:
143         commit = "release"
144
145     try:
146         text =  '#include "version.h"\n'
147         text += 'char const * dcp::git_commit = \"%s\";\n' % commit
148         text += 'char const * dcp::version = \"%s\";\n' % version
149         if bld.env.ENABLE_DEBUG:
150             debug_string = 'true'
151         else:
152             debug_string = 'false'
153         text += 'bool const built_with_debug = %s;\n' % debug_string
154         print('Writing version information to src/version.cc')
155         o = open('src/version.cc', 'w')
156         o.write(text)
157         o.close()
158     except IOError:
159         print('Could not open src/version.cc for writing\n')
160         sys.exit(-1)
161
162 def post(ctx):
163     if ctx.cmd == 'install':
164         ctx.exec_command('/sbin/ldconfig')