Debian Codegraph @

build_tooling/cmake/tangent/debian.cmake

raw
1 set(SUPPORTED_DISTRIBUTIONS xenial bionic eoan focal)
2 set(SUPPORTED_ARCHITECTURES amd64 arm64 i386)
3
4 foreach(distro ${SUPPORTED_DISTRIBUTIONS})
5 add_custom_target(${distro}-source-packages)
6 foreach(arch ${SUPPORTED_ARCHITECTURES})
7 add_custom_target(debs-${distro}-${arch})
8 endforeach()
9 if(BUILDENV_DPKG_ARCHITECTURE)
10 add_custom_target(debs-${distro})
11 add_dependencies(debs-${distro}
12 debs-${distro}-${BUILDENV_DPKG_ARCHITECTURE})
13 endif()
14 endforeach()
15
16 if(BUILDENV_DISTRIB_CODENAME AND BUILDENV_DPKG_ARCHITECTURE)
17 add_custom_target(debs)
18 add_dependencies(
19 debs debs-${BUILDENV_DISTRIB_CODENAME}-${BUILDENV_DPKG_ARCHITECTURE})
20 endif()
21
22 check_call(COMMAND id -u OUTPUT_VARIABLE _uid)
23 set(_sudo)
24 if(NOT "${_uid}" EQUAL "0")
25 set(_sudo "sudo")
26 endif()
27
28 # Return in ${outvar} a list of command line options added to the debuild
29 # command in order to sign the package. If DEBIAN_SIGNING_KEY is define it will
30 # return flags used to sign with that key. If it is not defined, it will return
31 # flags used to skip signing.
32 function(get_signing_flags outvar)
33 if(DEBIAN_SIGNING_KEY)
34 # e.g. 6A8A4FAF
35 set(${outvar}
36 -sa -pgpg2 -k${DEBIAN_SIGNING_KEY}
37 PARENT_SCOPE)
38 else()
39 # NOTE(josh):
40 #
41 # * -uc, --unsigned-changes
42 # * -us, --unsigned-source
43 set(${outvar}
44 -uc -us
45 PARENT_SCOPE)
46 endif()
47 endfunction()
48
49 # Given a list of variable names in the current scope, save them all as
50 # properties
51 #
52 # Usage:
53 # ~~~
54 # EXPORTVARS(<prop-spec> VARS <var1> [<var2 [...]])
55 # ~~~
56 #
57 # Example:
58 # ~~~
59 # EXPORTVARS(DIRECTORY VARS foo bar baz)
60 # ~~~
61 macro(EXPORTVARS)
62 cmake_parse_arguments(_args "" "" "VARS" ${ARGN})
63
64 foreach(varname ${_args_VARS})
65 set_property(${_args_UNPARSED_ARGUMENTS} PROPERTY ${varname}
66 "${${varname}}")
67 endforeach()
68 endmacro()
69
70 # Retrieve a list of properites as variables with the same in the current scope
71 #
72 # Usage:
73 # ~~~
74 # IMPORTVARS(<prop-spec> VARS <var1> [<var2 [...]])
75 # ~~~
76 #
77 # Example:
78 # ~~~
79 # IMPORTVARS(DIRECTORY VARS foo bar baz)
80 # ~~~
81 macro(IMPORTVARS)
82 cmake_parse_arguments(_args "" "" "VARS" ${ARGN})
83
84 foreach(varname ${_args_VARS})
85 get_property(${varname} ${_args_UNPARSED_ARGUMENTS} PROPERTY ${varname})
86 endforeach()
87 endmacro()
88
89 # Return the path to the pbuilder base image tarball for the given `distro` and
90 # `arch`.
91 function(get_pbuilder_basetgz outvar distro arch)
92 set(${outvar}
93 /var/cache/pbuilder/${distro}-${arch}-base.tgz
94 PARENT_SCOPE)
95 endfunction()
96
97 add_custom_target(
98 check-pbuilder-rc
99 COMMAND python -B ${TANGENT_TOOLING}/check_pbuilderrc.py
100 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
101
102 # Add a rule to generate the pbuilder base image tarball for the given `distro`
103 # and `arch`.
104 function(pbuilder_create distro arch)
105 get_pbuilder_basetgz(_basetgz ${distro} ${arch})
106 add_custom_command(
107 OUTPUT ${_basetgz}
108 DEPENDS check-pbuilder-rc
109 COMMAND
110 ${_sudo} pbuilder create --distribution ${distro} #
111 --architecture ${arch} --basetgz ${_basetgz} #
112 --hookdir ${CMAKE_SOURCE_DIR}/debian/pbuilder-hooks #
113 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
114 COMMENT "Bootstraping base tgz for ${distro}/${arch}")
115 add_custom_target(pbuilder-basetgz-${distro}-${arch} DEPENDS ${_basetgz})
116 endfunction()
117
118 set_property(GLOBAL PROPERTY DEBHELP_INCLUDENO 0)
119
120 # Run the debhelp python script, write the output to a unique file, then include
121 # the result file into the current cmake scope, executing it inline.
122 #
123 # Usage:
124 # ~~~
125 # debhelp(<command> [arg1 [arg2 [...]]])
126 # ~~~
127 macro(DEBHELP command)
128 get_property(_includeno GLOBAL PROPERTY DEBHELP_INCLUDENO)
129 math(EXPR _includeno "${_includeno} + 1")
130 set_property(GLOBAL PROPERTY DEBHELP_INCLUDENO ${_includeno})
131
132 set(_stubfile ${CMAKE_BINARY_DIR}/debian/debhelp-${_includeno}.cmake)
133 execute_process(
134 COMMAND python -B ${TANGENT_TOOLING}/debhelp.py -o ${_stubfile} ${command}
135 ${ARGN}
136 RESULT_VARIABLE _returncode
137 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
138 if(NOT _returncode EQUAL 0)
139 message(FATAL_ERROR "Failed to execute debhelp ${command}")
140 endif()
141 include(${_stubfile} RESULT_VARIABLE _result)
142 if(_result EQUAL NOTFOUND)
143 message(FATAL_ERROR "Failed to include debhelp stubfile ${_stubfile}")
144 endif()
145 endmacro()
146
147 # Run the debhelp python script to parse the changelog
148 #
149 # Usage:
150 # ~~~
151 # debhelp_parsechangelog(<filepath> <prefix>)
152 # ~~~
153 #
154 # Returns the following variables into the callers scope:
155 #
156 # * <prefix>_package: debian package name
157 # * <prefix>_version: upstream package version
158 # * <prefix>_debversion: debian version suffix
159 function(debhelp_parsechangelog filepath prefix)
160 debhelp(parse-changelog ${filepath} ${prefix})
161 # Enforces cmake re-run if the changelog changes. This is required because the
162 # version number might have changed and we'll need to reparse it.
163 configure_file(${filepath}
164 ${CMAKE_BINARY_DIR}/debhelp/parse-stamps/${filepath} COPYONLY)
165 exportvars(DIRECTORY VARS ${prefix}_package ${prefix}_version
166 ${prefix}_debversion)
167 endfunction()
168
169 # Create the "upstream" tarball used to generate a debian package. The tarball
170 # contains all of the "exported" sources as specified by a list of glob patterns
171 # in a file
172 #
173 # Usage:
174 # ~~~
175 # create_debian_tarball(<name>)
176 # ~~~
177 #
178 # Where `name` is a subdirectory of `debian/exports`.
179 function(create_debian_tarball name)
180 importvars(DIRECTORY VARS ${name}_package ${name}_version ${name}_debversion)
181 set(basename ${${name}_package}_${${name}_version})
182 set(common_patterns_path
183 ${CMAKE_CURRENT_SOURCE_DIR}/common/debian/sources.txt)
184 set(patterns_path
185 ${CMAKE_CURRENT_SOURCE_DIR}/exports/${name}/debian/sources.txt)
186 set(manifest_path ${CMAKE_CURRENT_BINARY_DIR}/${name}.manifest)
187 set(debfiles_path ${CMAKE_CURRENT_BINARY_DIR}/${name}.debfiles)
188
189 # Parse the sources.txt include globs and build a manifest of what source
190 # files to include in the tarball. Save this to ${manifest_path}
191 add_custom_target(
192 deb-chkmanifest-${name}
193 COMMAND
194 python -B ${TANGENT_TOOLING}/debhelp.py #
195 --outpath ${manifest_path} #
196 check-manifest
197 --patterns-from ${common_patterns_path}
198 --patterns-from ${patterns_path} ${CMAKE_SOURCE_DIR}
199 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
200 BYPRODUCTS ${manifest_path}
201 COMMENT "Checking source manifest for ${name}")
202
203 if(NOT ${CMAKE_GENERATOR} STREQUAL Ninja)
204 add_custom_command(
205 OUTPUT ${manifest_path}
206 COMMAND true
207 DEPENDS deb-chkmanifest-${name}
208 COMMENT "Stubbing source manifest for ${name}")
209 endif()
210
211 # Glob the common/debian and exports/${name}/debian directories to get a list
212 # of files that go in the debian/ directory of the tarball
213 add_custom_target(
214 deb-chkdebfiles-${name}
215 COMMAND
216 python -B ${TANGENT_TOOLING}/debhelp.py #
217 --outpath ${debfiles_path} #
218 check-manifest --patterns common/debian/* exports/${name}/debian/* --
219 ${CMAKE_CURRENT_SOURCE_DIR}
220 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
221 BYPRODUCTS ${debfiles_path}
222 COMMENT "Checking debian files manifest for ${name}")
223
224 if(NOT ${CMAKE_GENERATOR} STREQUAL Ninja)
225 add_custom_command(
226 OUTPUT ${debfiles_path}
227 COMMAND true
228 DEPENDS deb-chkdebfiles-${name}
229 COMMENT "Stubbing debian files manifest for ${name}")
230 endif()
231
232 # Create the source tarball. We do this just once and then simlink it into the
233 # different distribution directoreies.
234 add_custom_command(
235 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${basename}.orig.tar.gz
236 COMMAND
237 tar --create --gzip #
238 --file=${CMAKE_CURRENT_BINARY_DIR}/${basename}.orig.tar.gz #
239 --transform 's,^,${basename}/,' #
240 --directory ${CMAKE_CURRENT_SOURCE_DIR}/common . #
241 --directory ${CMAKE_CURRENT_SOURCE_DIR}/exports/${name} . #
242 --directory ${CMAKE_SOURCE_DIR} --files-from ${manifest_path}
243 DEPENDS ${manifest_path} ${debfiles_path}
244 COMMENT "Creating source tarball ${basename}.orig.tar.gz"
245 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
246 endfunction()
247
248 # Create a debian source package by translating the changelog from a generic
249 # channel name to an ubuntu-suite channel name and then running debuild
250 #
251 # Usage:
252 # ~~~
253 # create_debian_source_package(<name> <distro>)
254 # ~~~
255 #
256 # Where `name` is a subdirectory of `debian/exports` and `distro` is an valid
257 # ubuntu codename (xenial, bionic, focal).
258 function(create_debian_source_package name distro)
259 importvars(DIRECTORY VARS ${name}_package ${name}_version ${name}_debversion)
260 set(basename ${${name}_package}_${${name}_version})
261 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${distro})
262 set(changelog ${CMAKE_CURRENT_SOURCE_DIR}/exports/${name}/debian/changelog)
263
264 # Create a symlink to the upstream tarball within the distro-specific working
265 # directory
266 add_custom_command(
267 OUTPUT ${outdir}/${basename}.orig.tar.gz
268 COMMAND ${CMAKE_COMMAND} -E make_directory ${outdir}
269 COMMAND ${CMAKE_COMMAND} -E create_symlink ../${basename}.orig.tar.gz
270 ${outdir}/${basename}.orig.tar.gz
271 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
272 COMMENT "Creating ${distro} symlink to upstream tarball for ${name}"
273 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${basename}.orig.tar.gz)
274
275 # These are all the files that are created by debuild
276 set(outputs
277 ${outdir}/${basename}-${${name}_debversion}~${distro}.debian.tar.xz
278 ${outdir}/${basename}-${${name}_debversion}~${distro}.dsc
279 ${outdir}/${basename}-${${name}_debversion}~${distro}_source.build
280 ${outdir}/${basename}-${${name}_debversion}~${distro}_source.buildinfo
281 ${outdir}/${basename}-${${name}_debversion}~${distro}_source.changes)
282
283 get_signing_flags(_signflags)
284
285 # Extract the upstream source tarball, translate the changelog, and run
286 # debbuild
287 add_custom_command(
288 OUTPUT ${outputs}
289 # This is the directory where the upstream tarball will be extracted. We
290 # need to delete it so that we don't have any stale files
291 COMMAND rm -rf ${outdir}/${basename}
292 COMMAND tar --directory ${outdir} --extract --gzip
293 --file=${outdir}/${basename}.orig.tar.gz
294 # Copy the changelog to the distro-specific destination and replace the
295 # distribution name
296 COMMAND
297 python -B ${TANGENT_TOOLING}/debhelp.py translate-changelog #
298 --src ${changelog} #
299 --tgt ${outdir}/${basename}/debian/changelog #
300 --distro ${distro}
301 COMMAND
302 cd ${outdir}/${basename} #
303 && debuild -S -d ${_signflags} > ../${basename}.debuild1.log ||
304 (cat ../${basename}.debuild1.log && false)
305 DEPENDS ${outdir}/${basename}.orig.tar.gz
306 COMMENT "Creating ${distro} source package for ${name}"
307 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
308
309 add_custom_target(
310 dput-${name}-${distro}
311 COMMAND dput ppa:josh-bialkowski/tangent
312 ${outdir}/${basename}-${${name}_debversion}~${distro}_source.changes
313 DEPENDS ${outputs})
314
315 add_custom_target(deb-src-${name}-${distro} DEPENDS ${outputs})
316 add_dependencies(${distro}-source-packages deb-src-${name}-${distro})
317 endfunction()
318
319 # Create debian binary packages from the debian source package for `name`.
320 #
321 # Usage:
322 # ~~~
323 # create_debian_binary_packages(
324 # <tag> <distro> <arch> <binpkg1> [<binpkg2> [...]])
325 # ~~~
326 function(create_debian_binary_packages tag distro arch)
327 cmake_parse_arguments(_args "FORCE_PBUILDER" "" "OUTPUTS;DEPS" ${ARGN})
328
329 importvars(GLOBAL VARS NATIVE_ARCHITECTURE)
330 set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${distro})
331 set(basename "${${tag}_package}_${${tag}_version}")
332 set(version "${${tag}_version}-${${tag}_debversion}~${distro}")
333
334 get_pbuilder_basetgz(_basetgz ${distro} ${arch})
335
336 # This is a directory where we will copy all of the dependant debian packages
337 # and construct a local filesystem debian repository.
338 set(depsrepo ${CMAKE_CURRENT_BINARY_DIR}/${distro}/${arch}/${tag}-deps)
339
340 # Given a list of all the dependant projects, construct a list of all the .deb
341 # files that we depend on
342 set(deb_depends)
343 foreach(deptag ${_args_DEPS})
344 get_property(_deblist GLOBAL PROPERTY deblist-${deptag}-${distro}-${arch})
345 list(APPEND deb_depends ${_deblist})
346 endforeach()
347
348 # Create rules to copy all our dependant .deb files into a single directory
349 # which we will bind to the pbuilder chroot as a filesystem debian repository.
350 set(repo_debs)
351 foreach(deb ${deb_depends})
352 get_filename_component(_filename ${deb} NAME)
353 # NOTE(josh): we add the deps/ path suffix so that at least one component is
354 # in-common with the bindmount location. We'll need this to generate correct
355 # file paths in the Packages file.
356 set(repo_deb ${depsrepo}/deps/${_filename})
357 add_custom_command(
358 OUTPUT ${repo_deb}
359 COMMAND ${CMAKE_COMMAND} -E copy ${deb} ${repo_deb}
360 DEPENDS ${deb})
361 list(APPEND repo_debs ${repo_deb})
362 endforeach()
363
364 # Create a rule to generate the debian `Packages` file indexing the packages
365 # of our filesystem debian repository.
366 add_custom_command(
367 OUTPUT ${depsrepo}/deps/Packages
368 COMMAND dpkg-scanpackages . > Packages
369 # NOTE(josh): the following is equivalent. I'm not sure which tool is better
370 # to use??
371 #
372 # COMMAND apt-ftparchive packages . > Packages
373 WORKING_DIRECTORY ${depsrepo}/deps
374 DEPENDS ${repo_debs}
375 COMMENT "Creating depsrepo for ${tag}")
376
377 get_signing_flags(_signflags)
378
379 # ${outputs} contains a list of all the files that are generated as part of
380 # the binary packages build. It is generated from the `OUTPUTS` keyword
381 # argument by prepending the output directory and appending the version and
382 # architecture suffixes.
383 set(outputs)
384 foreach(binpkg ${_args_OUTPUTS})
385 set(debname ${binpkg}_${version}_${arch}.deb)
386 list(APPEND outputs ${outdir}/${debname})
387 endforeach()
388
389 if("${distro}" STREQUAL "${BUILDENV_DISTRIB_CODENAME}" #
390 AND "${arch}" STREQUAL "${NATIVE_ARCHITECTURE}"
391 AND NOT ${_args_FORCE_PBUILDER}
392 AND NOT _args_DEPS)
393 add_custom_command(
394 OUTPUT ${outputs}
395 # NOTE(josh): add the -nc, --no-pre-clean option to reuse the existing
396 # build directory for an incremental build.
397 COMMAND debuild -b ${_signflags} > ${outdir}/${basename}.debuild2.log ||
398 (cat ${outdir}/${basename}.debuild2.log && false)
399 WORKING_DIRECTORY ${outdir}/${basename}
400 DEPENDS ${outdir}/${basename}.orig.tar.gz
401 ${outdir}/${basename}-${${tag}_debversion}~${distro}.dsc
402 COMMENT "Creating (native) binary packages for ${tag}")
403 else()
404 add_custom_command(
405 OUTPUT ${outputs}
406 COMMAND
407 ${_sudo} env DEB_BUILD_OPTIONS="parallel=8" #
408 pbuilder build --distribution #
409 ${distro} --architecture ${arch} --basetgz ${_basetgz} #
410 --buildresult ${CMAKE_CURRENT_BINARY_DIR}/${distro} #
411 --hookdir ${CMAKE_CURRENT_SOURCE_DIR}/pbuilder-hooks #
412 --bindmounts "${depsrepo}/deps:/var/cache/pbuilder/deps" #
413 --loglevel W #
414 --logfile ${outdir}/${basename}.pbuild.log #
415 ${outdir}/${basename}-${${tag}_debversion}~${distro}.dsc #
416 > /dev/null #
417 || (cat ${outdir}/${basename}.pbuild.log && false)
418 DEPENDS ${outdir}/${basename}.orig.tar.gz
419 ${outdir}/${basename}-${${tag}_debversion}~${distro}.dsc
420 ${_basetgz} ${depsrepo}/deps/Packages check-pbuilder-rc
421 COMMENT "Creating ${distro}/${arch} (pbuilder) binary pkgs for ${tag}")
422 endif()
423
424 add_custom_target(debs-${tag}-${distro}-${arch} DEPENDS ${outputs})
425 add_dependencies(debs-${distro}-${arch} debs-${tag}-${distro}-${arch})
426 set_property(GLOBAL PROPERTY deblist-${tag}-${distro}-${arch} ${outputs})
427 endfunction()
428
429 # Entry point into the debian package build system. The build system is roughly
430 # summarized as:
431 #
432 # 1. Parse the changelog to get version numbers
433 # 2. Create an "upstream" tarball
434 # 3. Foreach distribution: a. Create a debian source package b. Foreach
435 # architecture: i. create debian binary packages
436 #
437 # Usage:
438 # ~~~
439 # create_debian_packages(<tag> <binpkg1> [<binpkg2> [...]])
440 # ~~~
441 function(create_debian_packages tag)
442 set(changelog ${CMAKE_CURRENT_SOURCE_DIR}/exports/${tag}/debian/changelog)
443 debhelp_parsechangelog(${changelog} ${tag})
444 create_debian_tarball(${tag})
445
446 importvars(DIRECTORY VARS ${tag}_package ${tag}_version ${tag}_debversion)
447
448 foreach(distro ${SUPPORTED_DISTRIBUTIONS})
449 create_debian_source_package(${tag} ${distro})
450 foreach(arch ${SUPPORTED_ARCHITECTURES})
451 create_debian_binary_packages(${tag} ${distro} ${arch} ${ARGN})
452 endforeach()
453 endforeach()
454 endfunction()
455
456 foreach(distro ${SUPPORTED_DISTRIBUTIONS})
457 foreach(arch ${SUPPORTED_ARCHITECTURES})
458 pbuilder_create(${distro} ${arch})
459 endforeach()
460 endforeach()
461
462 # Return in ${outvar} a list of all the binary packages produced by the list of
463 # slugs.
464 #
465 # Usage:
466 # ~~~
467 # get_debs(<outvar> <distro> <arch> <slug1> [<slug2> [<slug3> [...]]])
468 # ~~~
469 function(get_debs outvar distro arch)
470 set(deb_depends)
471 foreach(slug tangent-util json argue linkhash)
472 get_property(deblist GLOBAL PROPERTY deblist-${slug}-${distro}-${arch})
473 list(APPEND deb_depends ${deblist})
474 endforeach()
475 set(${outvar}
476 ${deb_depends}
477 PARENT_SCOPE)
478 endfunction()