1# Copyright 2019 The Meson development team 2 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6 7# http://www.apache.org/licenses/LICENSE-2.0 8 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import argparse 16 17meson_executable_template = '''project('{project_name}', '{language}', 18 version : '{version}', 19 default_options : [{default_options}]) 20 21executable('{executable}', 22 {sourcespec},{depspec} 23 install : true) 24''' 25 26 27meson_jar_template = '''project('{project_name}', '{language}', 28 version : '{version}', 29 default_options : [{default_options}]) 30 31jar('{executable}', 32 {sourcespec},{depspec} 33 main_class: '{main_class}', 34 install : true) 35''' 36 37 38def create_meson_build(options: argparse.Namespace) -> None: 39 if options.type != 'executable': 40 raise SystemExit('\nGenerating a meson.build file from existing sources is\n' 41 'supported only for project type "executable".\n' 42 'Run meson init in an empty directory to create a sample project.') 43 default_options = ['warning_level=3'] 44 if options.language == 'cpp': 45 # This shows how to set this very common option. 46 default_options += ['cpp_std=c++14'] 47 # If we get a meson.build autoformatter one day, this code could 48 # be simplified quite a bit. 49 formatted_default_options = ', '.join(f"'{x}'" for x in default_options) 50 sourcespec = ',\n '.join(f"'{x}'" for x in options.srcfiles) 51 depspec = '' 52 if options.deps: 53 depspec = '\n dependencies : [\n ' 54 depspec += ',\n '.join(f"dependency('{x}')" 55 for x in options.deps.split(',')) 56 depspec += '],' 57 if options.language != 'java': 58 content = meson_executable_template.format(project_name=options.name, 59 language=options.language, 60 version=options.version, 61 executable=options.executable, 62 sourcespec=sourcespec, 63 depspec=depspec, 64 default_options=formatted_default_options) 65 else: 66 content = meson_jar_template.format(project_name=options.name, 67 language=options.language, 68 version=options.version, 69 executable=options.executable, 70 main_class=options.name, 71 sourcespec=sourcespec, 72 depspec=depspec, 73 default_options=formatted_default_options) 74 open('meson.build', 'w', encoding='utf-8').write(content) 75 print('Generated meson.build file:\n\n' + content) 76