Integrate parts of meta-intel-iot-security
[AGL/meta-agl.git] / meta-security / classes / deploy-files.bbclass
1 DEPLOY_FILES_DIR = "${WORKDIR}/deploy-files-${PN}"
2 SSTATETASKS += "do_deploy_files"
3 do_deploy_files[sstate-inputdirs] = "${DEPLOY_FILES_DIR}"
4 do_deploy_files[sstate-outputdirs] = "${DEPLOY_DIR}/files/"
5
6 python do_deploy_files_setscene () {
7     sstate_setscene(d)
8 }
9 addtask do_deploy_files_setscene
10 do_deploy_files[dirs] = "${DEPLOY_FILES_DIR} ${B}"
11
12 # Use like this:
13 # DEPLOY_FILES = "abc xyz"
14 # DEPLOY_FILES_FROM[abc] = "file-ab dir-c"
15 # DEPLOY_FILES_TO[abc] = "directory-for-abc"
16 # DEPLOY_FILES_FROM[xyz] = "file-xyz"
17 # DEPLOY_FILES_TO[xyz] = "directory-for-xyz"
18 #
19 # The destination directory will be created inside
20 # ${DEPLOYDIR}. The source files and directories
21 # will be copied such that their name and (for
22 # directories) the directory tree below it will
23 # be preserved. Shell wildcards are supported.
24 #
25 # The default DEPLOY_FILES copies files for the native host
26 # and the target into two different directories. Use that as follows:
27 # DEPLOY_FILES_FROM_native = "native-file"
28 # DEPLOY_FILES_FROM_target = "target-file"
29
30 DEPLOY_FILES ?= "native target"
31 DEPLOY_FILES_FROM[native] ?= ""
32 DEPLOY_FILES_TO[native] = "native/${BUILD_ARCH}"
33 DEPLOY_FILES_FROM[target] ?= ""
34 DEPLOY_FILES_TO[target] = "target/${MACHINE}"
35
36 # We have to use a Python function to access variable flags. Because
37 # bitbake then does not know about the dependency on these variables,
38 # we need to explicitly declare that. DEPLOYDIR may change without
39 # invalidating the sstate, therefore it is not listed.
40 do_deploy_files[vardeps] = "DEPLOY_FILES DEPLOY_FILES_FROM DEPLOY_FILES_TO"
41 python do_deploy_files () {
42     import glob
43     import os
44     import shutil
45
46     for file in (d.getVar('DEPLOY_FILES', True) or '').split():
47         bb.note('file: %s' % file)
48         from_pattern = d.getVarFlag('DEPLOY_FILES_FROM', file, True)
49         bb.note('from: %s' % from_pattern)
50         if from_pattern:
51             to = os.path.join(d.getVar('DEPLOY_FILES_DIR', True), d.getVarFlag('DEPLOY_FILES_TO', file, True))
52             bb.note('to: %s' % to)
53             if not os.path.isdir(to):
54                 os.makedirs(to)
55             for from_path in from_pattern.split():
56                 for src in (glob.glob(from_path) or [from_path]):
57                     bb.note('Deploying %s to %s' % (src, to))
58                     if os.path.isdir(src):
59                         src_dirname = shutil._basename(src)
60                         to = os.path.join(to, src_dirname)
61                         if os.path.exists(to):
62                             bb.utils.remove(to, True)
63                         shutil.copytree(src, to)
64                     else:
65                         shutil.copy(src, to)
66 }
67
68 addtask deploy_files before do_build after do_compile