| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019 The Meson development team |
| 3 |
# Copyright © 2023-2025 Intel Corporation |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
import typing as T |
| 7 |
|
| 8 |
from mesonbuild.templates.sampleimpl import FileHeaderImpl |
| 9 |
|
| 10 |
if T.TYPE_CHECKING: |
| 11 |
from ..minit import Arguments |
| 12 |
|
| 13 |
|
| 14 |
lib_h_template = '''#pragma once |
| 15 |
#if defined _WIN32 || defined __CYGWIN__ |
| 16 |
#ifdef BUILDING_{utoken} |
| 17 |
#define {utoken}_PUBLIC __declspec(dllexport) |
| 18 |
#else |
| 19 |
#define {utoken}_PUBLIC __declspec(dllimport) |
| 20 |
#endif |
| 21 |
#elif defined __OS2__ |
| 22 |
#ifdef BUILDING_{utoken} |
| 23 |
#define {utoken}_PUBLIC __declspec(dllexport) |
| 24 |
#else |
| 25 |
#define {utoken}_PUBLIC |
| 26 |
#endif |
| 27 |
#else |
| 28 |
#ifdef BUILDING_{utoken} |
| 29 |
#define {utoken}_PUBLIC __attribute__ ((visibility ("default"))) |
| 30 |
#else |
| 31 |
#define {utoken}_PUBLIC |
| 32 |
#endif |
| 33 |
#endif |
| 34 |
|
| 35 |
int {utoken}_PUBLIC {function_name}(); |
| 36 |
|
| 37 |
''' |
| 38 |
|
| 39 |
lib_objcpp_template = '''#import <{header_file}> |
| 40 |
|
| 41 |
/* This function will not be exported and is not |
| 42 |
* directly callable by users of this library. |
| 43 |
*/ |
| 44 |
int internal_function() {{ |
| 45 |
return 0; |
| 46 |
}} |
| 47 |
|
| 48 |
int {function_name}() {{ |
| 49 |
return internal_function(); |
| 50 |
}} |
| 51 |
''' |
| 52 |
|
| 53 |
lib_objcpp_test_template = '''#import <{header_file}> |
| 54 |
#import <iostream> |
| 55 |
|
| 56 |
int main(int argc, char **argv) {{ |
| 57 |
if (argc != 1) {{ |
| 58 |
std::cout << argv[0] << " takes no arguments." << std::endl; |
| 59 |
return 1; |
| 60 |
}} |
| 61 |
return {function_name}(); |
| 62 |
}} |
| 63 |
''' |
| 64 |
|
| 65 |
lib_objcpp_meson_template = '''project( |
| 66 |
'{project_name}', |
| 67 |
'objcpp', |
| 68 |
version : '{version}', |
| 69 |
meson_version : '>= {meson_version}', |
| 70 |
default_options : ['warning_level=3'], |
| 71 |
) |
| 72 |
|
| 73 |
dependencies = [{dependencies} |
| 74 |
] |
| 75 |
|
| 76 |
sources = [{source_files} |
| 77 |
] |
| 78 |
|
| 79 |
# These arguments are only used to build the shared library |
| 80 |
# not the executables that use the library. |
| 81 |
lib_args = ['-DBUILDING_{utoken}'] |
| 82 |
|
| 83 |
lib = library( |
| 84 |
'{lib_name}', |
| 85 |
sources, |
| 86 |
install : true, |
| 87 |
objcpp_shared_args : lib_args, |
| 88 |
dependencies : dependencies, |
| 89 |
gnu_symbol_visibility : 'hidden', |
| 90 |
) |
| 91 |
|
| 92 |
test_exe = executable( |
| 93 |
'{test_exe_name}', |
| 94 |
'{test_source_file}', |
| 95 |
dependencies : dependencies, |
| 96 |
link_with : lib, |
| 97 |
) |
| 98 |
test('{test_name}', test_exe) |
| 99 |
|
| 100 |
# Make this library usable as a Meson subproject. |
| 101 |
{ltoken}_dep = declare_dependency( |
| 102 |
include_directories : include_directories('.'), |
| 103 |
dependencies : dependencies, |
| 104 |
link_with : lib, |
| 105 |
) |
| 106 |
meson.override_dependency('{project_name}', {ltoken}_dep) |
| 107 |
|
| 108 |
# Make this library usable from the system's |
| 109 |
# package manager. |
| 110 |
install_headers('{header_file}', subdir : '{header_dir}') |
| 111 |
|
| 112 |
pkg_mod = import('pkgconfig') |
| 113 |
pkg_mod.generate( |
| 114 |
lib, |
| 115 |
description : 'Meson sample project.', |
| 116 |
subdirs : '{header_dir}', |
| 117 |
) |
| 118 |
''' |
| 119 |
|
| 120 |
hello_objcpp_template = '''#import <iostream> |
| 121 |
|
| 122 |
#define PROJECT_NAME "{project_name}" |
| 123 |
|
| 124 |
int main(int argc, char **argv) {{ |
| 125 |
if (argc != 1) {{ |
| 126 |
std::cout << argv[0] << " takes no arguments." << std::endl; |
| 127 |
return 1; |
| 128 |
}} |
| 129 |
std::cout << "This is project " << PROJECT_NAME << "." << std::endl; |
| 130 |
return 0; |
| 131 |
}} |
| 132 |
''' |
| 133 |
|
| 134 |
hello_objcpp_meson_template = '''project( |
| 135 |
'{project_name}', |
| 136 |
'objcpp', |
| 137 |
version : '{version}', |
| 138 |
meson_version : '>= {meson_version}', |
| 139 |
default_options : ['warning_level=3'], |
| 140 |
) |
| 141 |
|
| 142 |
dependencies = [{dependencies} |
| 143 |
] |
| 144 |
|
| 145 |
sources = [{source_files} |
| 146 |
] |
| 147 |
|
| 148 |
exe = executable( |
| 149 |
'{exe_name}', |
| 150 |
sources, |
| 151 |
dependencies : dependencies, |
| 152 |
install : true, |
| 153 |
) |
| 154 |
|
| 155 |
test('basic', exe) |
| 156 |
''' |
| 157 |
|
| 158 |
|
| 159 |
class ObjCppProject(FileHeaderImpl): |
| 160 |
|
| 161 |
source_ext = 'mm' |
| 162 |
header_ext = 'h' |
| 163 |
exe_template = hello_objcpp_template |
| 164 |
exe_meson_template = hello_objcpp_meson_template |
| 165 |
lib_template = lib_objcpp_template |
| 166 |
lib_header_template = lib_h_template |
| 167 |
lib_test_template = lib_objcpp_test_template |
| 168 |
lib_meson_template = lib_objcpp_meson_template |
| 169 |
|
| 170 |
def __init__(self, args: Arguments): |
| 171 |
super().__init__(args) |
| 172 |
self.meson_version = '1.3.0' |