agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 3 | # Copyright (C) 2020 Savoir-faire Linux Inc. |
| 4 | # |
| 5 | # Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation, either version 3 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | # Creates packaging targets for a distribution and architecture. |
| 21 | # This helps reduce the length of the top Makefile. |
| 22 | # |
| 23 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 24 | # This script must unify the Plugins build for |
| 25 | # every project and Operational System |
| 26 | |
| 27 | import os |
| 28 | import sys |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 29 | import json |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 30 | import platform |
| 31 | import argparse |
| 32 | import subprocess |
| 33 | import multiprocessing |
| 34 | |
| 35 | IOS_DISTRIBUTION_NAME = "ios" |
| 36 | OSX_DISTRIBUTION_NAME = "osx" |
| 37 | ANDROID_DISTRIBUTION_NAME = "android" |
| 38 | WIN32_DISTRIBUTION_NAME = "win32" |
| 39 | UBUNTU_DISTRIBUTION_NAME = "ubuntu" |
| 40 | |
| 41 | # vs vars |
| 42 | win_sdk_default = '10.0.16299.0' |
| 43 | win_toolset_default = 'v142' |
| 44 | |
| 45 | |
| 46 | def parse(): |
| 47 | parser = argparse.ArgumentParser(description='Builds Plugins projects') |
| 48 | parser.add_argument('--projects', type=str, |
| 49 | help='Select plugins to be build.') |
| 50 | parser.add_argument('--distribution') |
| 51 | parser.add_argument('--processor', type=str, default="GPU", |
| 52 | help='Runtime plugin CPU/GPU setting.') |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 53 | parser.add_argument('--buildOptions', default='', type=str, |
| 54 | help="Type all build optionsto pass to package.json 'defines' property.\nThis argument consider that you're using cmake.") |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 55 | |
| 56 | dist = choose_distribution() |
| 57 | |
| 58 | if dist == WIN32_DISTRIBUTION_NAME: |
| 59 | parser.add_argument('--toolset', default=win_toolset_default, type=str, |
| 60 | help='Windows use only, specify Visual Studio toolset version') |
| 61 | parser.add_argument('--sdk', default=win_sdk_default, type=str, |
| 62 | help='Windows use only, specify Windows SDK version') |
| 63 | |
| 64 | args = parser.parse_args() |
| 65 | args.projects = args.projects.split(',') |
| 66 | args.processor = args.processor.split(',') |
| 67 | |
| 68 | if (args.distribution is not None): |
| 69 | args.distribution = args.distribution.lower() |
| 70 | else: |
| 71 | args.distribution = dist |
| 72 | |
| 73 | if (len(args.processor) == 1): |
| 74 | args.processor *= len(args.projects) |
| 75 | |
| 76 | validate_args(args) |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 77 | |
| 78 | if dist != WIN32_DISTRIBUTION_NAME: |
| 79 | args.toolset = '' |
| 80 | args.sdk = '' |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 81 | return args |
| 82 | |
| 83 | |
| 84 | def validate_args(parsed_args): |
| 85 | """Validate the args values, exit if error is found""" |
| 86 | |
| 87 | # Filter unsupported distributions. |
| 88 | supported_distros = [ |
| 89 | ANDROID_DISTRIBUTION_NAME, UBUNTU_DISTRIBUTION_NAME, |
| 90 | WIN32_DISTRIBUTION_NAME |
| 91 | ] |
| 92 | |
| 93 | if parsed_args.distribution not in supported_distros: |
| 94 | print('Distribution \'{0}\' not supported.\nChoose one of: {1}'.format( |
| 95 | parsed_args.distribution, ', '.join(supported_distros) |
| 96 | )) |
| 97 | sys.exit(1) |
| 98 | |
| 99 | if (len(parsed_args.processor) != len(parsed_args.projects)): |
| 100 | sys.exit('Processor must be single or the same size as projects.') |
| 101 | |
| 102 | for processor in parsed_args.processor: |
| 103 | if (processor not in ['GPU', 'CPU']): |
| 104 | sys.exit('Processor can only be GPU or CPU.') |
| 105 | |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 106 | if (parsed_args.buildOptions): |
| 107 | parsed_args.buildOptions = parsed_args.buildOptions.split(',') |
| 108 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 109 | |
| 110 | def choose_distribution(): |
| 111 | system = platform.system().lower() |
| 112 | |
| 113 | if system == "linux" or system == "linux2": |
| 114 | if os.path.isfile("/etc/arch-release"): |
| 115 | return "arch" |
| 116 | with open("/etc/os-release") as f: |
| 117 | for line in f: |
| 118 | k, v = line.split("=") |
| 119 | if k.strip() == 'ID': |
| 120 | return v.strip().replace('"', '').split(' ')[0] |
| 121 | elif system == "darwin": |
| 122 | return OSX_DISTRIBUTION_NAME |
| 123 | elif system == "windows": |
| 124 | return WIN32_DISTRIBUTION_NAME |
| 125 | |
| 126 | return 'Unknown' |
| 127 | |
| 128 | |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 129 | def buildPlugin(pluginPath, processor, distribution, toolset='', sdk='', buildOptions=''): |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 130 | if distribution == WIN32_DISTRIBUTION_NAME: |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 131 | if (buildOptions): |
| 132 | with open(f"{pluginPath}/package.json") as f: |
| 133 | defaultPackage = json.load(f) |
| 134 | package = defaultPackage.copy() |
| 135 | package['defines'] = [] |
| 136 | for option in buildOptions: |
| 137 | package['defines'].append(option) |
| 138 | f.close() |
| 139 | with open(f"{pluginPath}/package.json", 'w') as f: |
| 140 | json.dump(package, f, indent=4) |
| 141 | f.close() |
| 142 | subprocess.run([ |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 143 | sys.executable, os.path.join( |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 144 | os.getcwd(), "../../daemon/compat/msvc/winmake.py"), |
| 145 | "-P", |
| 146 | "--toolset", toolset, |
| 147 | "--sdk", sdk, |
| 148 | "-fb", pluginPath.split('/')[-1] |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 149 | ], check=True) |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 150 | if (buildOptions): |
| 151 | with open(f"{pluginPath}/package.json", "w") as f: |
| 152 | json.dump(defaultPackage, f, indent=4) |
| 153 | f.close() |
| 154 | return |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 155 | |
| 156 | environ = os.environ.copy() |
| 157 | |
| 158 | install_args = [] |
| 159 | |
| 160 | if distribution == ANDROID_DISTRIBUTION_NAME: |
| 161 | install_args.append('-t') |
| 162 | install_args.append(ANDROID_DISTRIBUTION_NAME) |
| 163 | if processor: |
| 164 | install_args.append('-c') |
| 165 | install_args.append(processor) |
| 166 | install_args.append('-p') |
| 167 | install_args.append(str(multiprocessing.cpu_count())) |
| 168 | |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 169 | subprocess.check_call(['chmod', '+x', pluginPath + "/build.sh"]) |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 170 | return subprocess.run([pluginPath + "/build.sh"] + |
| 171 | install_args, env=environ, check=True) |
| 172 | |
| 173 | |
| 174 | def main(): |
| 175 | args = parse() |
| 176 | currentDir = os.getcwd() |
| 177 | |
| 178 | for i, plugin in enumerate(args.projects): |
| 179 | os.chdir(currentDir + "/" + plugin) |
| 180 | buildPlugin( |
| 181 | currentDir + "/" + plugin, |
| 182 | args.processor[i], |
agsantos | 1e7736c | 2020-10-28 14:39:13 -0400 | [diff] [blame] | 183 | args.distribution, |
| 184 | args.toolset, |
| 185 | args.sdk, |
| 186 | args.buildOptions) |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 187 | |
| 188 | |
| 189 | if __name__ == "__main__": |
| 190 | main() |