Debian Codegraph @

mesonbuild/templates/fortrantemplates.py

raw
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 FileImpl
9
10 if T.TYPE_CHECKING:
11 from ..minit import Arguments
12
13 lib_fortran_template = '''
14 ! This procedure will not be exported and is not
15 ! directly callable by users of this library.
16
17 module modfoo
18
19 implicit none
20 private
21 public :: {function_name}
22
23 contains
24
25 integer function internal_function()
26 internal_function = 0
27 end function internal_function
28
29 integer function {function_name}()
30 {function_name} = internal_function()
31 end function {function_name}
32
33 end module modfoo
34 '''
35
36 lib_fortran_test_template = '''
37 use modfoo
38
39 print *,{function_name}()
40
41 end program
42 '''
43
44 lib_fortran_meson_template = '''project(
45 '{project_name}',
46 'fortran',
47 version : '{version}',
48 meson_version : '>= {meson_version}',
49 default_options : ['warning_level=3'],
50 )
51
52 # These arguments are only used to build the shared library
53 # not the executables that use the library.
54 lib_args = ['-DBUILDING_{utoken}']
55
56 dependencies = [{dependencies}
57 ]
58
59 sources = [{source_files}
60 ]
61
62 lib = library(
63 '{lib_name}',
64 sources,
65 install : true,
66 fortran_shared_args : lib_args,
67 gnu_symbol_visibility : 'hidden',
68 dependencies : dependencies,
69 )
70
71 test_exe = executable(
72 '{test_exe_name}',
73 '{test_source_file}',
74 link_with : lib,
75 dependencies : dependencies,
76 )
77 test('{test_name}', test_exe)
78
79 # Make this library usable as a Meson subproject.
80 {ltoken}_dep = declare_dependency(
81 include_directories : include_directories('.'),
82 dependencies : dependencies,
83 link_with : lib,
84 )
85 meson.override_dependency('{project_name}', {ltoken}_dep)
86
87 pkg_mod = import('pkgconfig')
88 pkg_mod.generate(
89 lib,
90 description : 'Meson sample project.',
91 subdirs : '{header_dir}',
92 )
93 '''
94
95 hello_fortran_template = '''
96 implicit none
97
98 character(len=*), parameter :: PROJECT_NAME = "{project_name}"
99
100 print *,"This is project ", PROJECT_NAME
101
102 end program
103 '''
104
105 hello_fortran_meson_template = '''project(
106 '{project_name}',
107 'fortran',
108 version : '{version}',
109 meson_version : '>= {meson_version}',
110 default_options : ['warning_level=3'],
111 )
112
113 dependencies = [{dependencies}
114 ]
115
116 sources = [{source_files}
117 ]
118
119 exe = executable(
120 '{exe_name}',
121 sources,
122 dependencies : dependencies,
123 install : true,
124 )
125
126 test('basic', exe)
127 '''
128
129
130 class FortranProject(FileImpl):
131
132 source_ext = 'f90'
133 exe_template = hello_fortran_template
134 exe_meson_template = hello_fortran_meson_template
135 lib_template = lib_fortran_template
136 lib_meson_template = lib_fortran_meson_template
137 lib_test_template = lib_fortran_test_template
138
139 def __init__(self, args: Arguments):
140 super().__init__(args)
141 self.meson_version = '1.3.0'