| 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 |
hello_cpp_template = '''#include <iostream> |
| 14 |
|
| 15 |
#define PROJECT_NAME "{project_name}" |
| 16 |
|
| 17 |
int main(int argc, char **argv) {{ |
| 18 |
if (argc != 1) {{ |
| 19 |
std::cout << argv[0] << " takes no arguments.\\n"; |
| 20 |
return 1; |
| 21 |
}} |
| 22 |
std::cout << "This is project " << PROJECT_NAME << ".\\n"; |
| 23 |
return 0; |
| 24 |
}} |
| 25 |
''' |
| 26 |
|
| 27 |
hello_cpp_meson_template = '''project( |
| 28 |
'{project_name}', |
| 29 |
'cpp', |
| 30 |
version : '{version}', |
| 31 |
meson_version : '>= {meson_version}', |
| 32 |
default_options : ['warning_level=3', 'cpp_std=c++14'], |
| 33 |
) |
| 34 |
|
| 35 |
dependencies = [{dependencies} |
| 36 |
] |
| 37 |
|
| 38 |
sources = [{source_files} |
| 39 |
] |
| 40 |
|
| 41 |
exe = executable( |
| 42 |
'{exe_name}', |
| 43 |
sources, |
| 44 |
install : true, |
| 45 |
dependencies : dependencies, |
| 46 |
) |
| 47 |
|
| 48 |
test('basic', exe) |
| 49 |
''' |
| 50 |
|
| 51 |
lib_hpp_template = '''#pragma once |
| 52 |
#if defined _WIN32 || defined __CYGWIN__ |
| 53 |
#ifdef BUILDING_{utoken} |
| 54 |
#define {utoken}_PUBLIC __declspec(dllexport) |
| 55 |
#else |
| 56 |
#define {utoken}_PUBLIC __declspec(dllimport) |
| 57 |
#endif |
| 58 |
#elif defined __OS2__ |
| 59 |
#ifdef BUILDING_{utoken} |
| 60 |
#define {utoken}_PUBLIC __declspec(dllexport) |
| 61 |
#else |
| 62 |
#define {utoken}_PUBLIC |
| 63 |
#endif |
| 64 |
#else |
| 65 |
#ifdef BUILDING_{utoken} |
| 66 |
#define {utoken}_PUBLIC __attribute__ ((visibility ("default"))) |
| 67 |
#else |
| 68 |
#define {utoken}_PUBLIC |
| 69 |
#endif |
| 70 |
#endif |
| 71 |
|
| 72 |
namespace {namespace} {{ |
| 73 |
|
| 74 |
class {utoken}_PUBLIC {class_name} {{ |
| 75 |
|
| 76 |
public: |
| 77 |
{class_name}(); |
| 78 |
int get_number() const; |
| 79 |
|
| 80 |
private: |
| 81 |
|
| 82 |
int number; |
| 83 |
|
| 84 |
}}; |
| 85 |
|
| 86 |
}} |
| 87 |
|
| 88 |
''' |
| 89 |
|
| 90 |
lib_cpp_template = '''#include <{header_file}> |
| 91 |
|
| 92 |
namespace {namespace} {{ |
| 93 |
|
| 94 |
{class_name}::{class_name}() {{ |
| 95 |
number = 6; |
| 96 |
}} |
| 97 |
|
| 98 |
int {class_name}::get_number() const {{ |
| 99 |
return number; |
| 100 |
}} |
| 101 |
|
| 102 |
}} |
| 103 |
''' |
| 104 |
|
| 105 |
lib_cpp_test_template = '''#include <{header_file}> |
| 106 |
#include <iostream> |
| 107 |
|
| 108 |
int main(int argc, char **argv) {{ |
| 109 |
if (argc != 1) {{ |
| 110 |
std::cout << argv[0] << " takes no arguments.\\n"; |
| 111 |
return 1; |
| 112 |
}} |
| 113 |
{namespace}::{class_name} c; |
| 114 |
return c.get_number() != 6; |
| 115 |
}} |
| 116 |
''' |
| 117 |
|
| 118 |
lib_cpp_meson_template = '''project( |
| 119 |
'{project_name}', |
| 120 |
'cpp', |
| 121 |
version : '{version}', |
| 122 |
meson_version : '>= {meson_version}', |
| 123 |
default_options : ['warning_level=3', 'cpp_std=c++14'], |
| 124 |
) |
| 125 |
|
| 126 |
dependencies = [{dependencies} |
| 127 |
] |
| 128 |
|
| 129 |
# These arguments are only used to build the shared library |
| 130 |
# not the executables that use the library. |
| 131 |
lib_args = ['-DBUILDING_{utoken}'] |
| 132 |
|
| 133 |
sources = [{source_files} |
| 134 |
] |
| 135 |
|
| 136 |
lib = library( |
| 137 |
'{lib_name}', |
| 138 |
sources, |
| 139 |
install : true, |
| 140 |
cpp_shared_args : lib_args, |
| 141 |
gnu_symbol_visibility : 'hidden', |
| 142 |
dependencies : dependencies, |
| 143 |
) |
| 144 |
|
| 145 |
test_exe = executable( |
| 146 |
'{test_exe_name}', |
| 147 |
'{test_source_file}', |
| 148 |
dependencies : dependencies, |
| 149 |
link_with : lib, |
| 150 |
) |
| 151 |
test('{test_name}', test_exe) |
| 152 |
|
| 153 |
# Make this library usable as a Meson subproject. |
| 154 |
{ltoken}_dep = declare_dependency( |
| 155 |
include_directories : include_directories('.'), |
| 156 |
dependencies : dependencies, |
| 157 |
link_with : lib, |
| 158 |
) |
| 159 |
meson.override_dependency('{project_name}', {ltoken}_dep) |
| 160 |
|
| 161 |
# Make this library usable from the system's |
| 162 |
# package manager. |
| 163 |
install_headers('{header_file}', subdir : '{header_dir}') |
| 164 |
|
| 165 |
pkg_mod = import('pkgconfig') |
| 166 |
pkg_mod.generate( |
| 167 |
lib, |
| 168 |
description : 'Meson sample project.', |
| 169 |
subdirs : '{header_dir}', |
| 170 |
) |
| 171 |
''' |
| 172 |
|
| 173 |
|
| 174 |
class CppProject(FileHeaderImpl): |
| 175 |
|
| 176 |
source_ext = 'cpp' |
| 177 |
header_ext = 'hpp' |
| 178 |
exe_template = hello_cpp_template |
| 179 |
exe_meson_template = hello_cpp_meson_template |
| 180 |
lib_template = lib_cpp_template |
| 181 |
lib_header_template = lib_hpp_template |
| 182 |
lib_test_template = lib_cpp_test_template |
| 183 |
lib_meson_template = lib_cpp_meson_template |
| 184 |
|
| 185 |
def __init__(self, args: Arguments): |
| 186 |
super().__init__(args) |
| 187 |
self.meson_version = '1.3.0' |