1#
2# SPDX-License-Identifier: BSD-3-Clause
3#
4# Copyright © 2019 Keith Packard
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#
13# 2. Redistributions in binary form must reproduce the above
14#    copyright notice, this list of conditions and the following
15#    disclaimer in the documentation and/or other materials provided
16#    with the distribution.
17#
18# 3. Neither the name of the copyright holder nor the names of its
19#    contributors may be used to endorse or promote products derived
20#    from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33# OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35
36src_picocrt = []
37
38machine_dir = 'machine' / host_cpu_family
39picocrt_march_add=''
40if fs.is_dir(machine_dir)
41  subdir(machine_dir)
42else
43  src_picocrt = files('shared/crt0.c')
44endif
45
46foreach target : targets
47  value = get_variable('target_' + target)
48
49  instdir = join_paths(lib_dir, value[0])
50
51  if picocrt_march_add != ''
52    new_cflags = []
53    foreach cflag : value[1]
54      if cflag.startswith('-march') and not cflag.contains(picocrt_march_add)
55	cflag = cflag + picocrt_march_add
56      endif
57      new_cflags += cflag
58    endforeach
59    value = [value[0], new_cflags]
60  endif
61
62  if target == ''
63    crt_name = 'crt0.o'
64    crt_hosted_name = 'crt0-hosted.o'
65    crt_minimal_name = 'crt0-minimal.o'
66    crt_semihost_name = 'crt0-semihost.o'
67    libcrt_name = 'crt0'
68    libcrt_hosted_name = 'crt0-hosted'
69    libcrt_minimal_name = 'crt0-minimal'
70    libcrt_semihost_name = 'crt0-semihost'
71  else
72    crt_name = join_paths(target, 'crt0.o')
73    crt_hosted_name = join_paths(target, 'crt0-hosted.o')
74    crt_minimal_name = join_paths(target, 'crt0-minimal.o')
75    crt_semihost_name = join_paths(target, 'crt0-semihost.o')
76    libcrt_name = join_paths(target, 'libcrt0')
77    libcrt_hosted_name = join_paths(target, 'libcrt0-hosted')
78    libcrt_minimal_name = join_paths(target, 'libcrt0-minimal')
79    libcrt_semihost_name = join_paths(target, 'libcrt0-semihost')
80  endif
81
82  crt0_name = 'crt0' + target
83  crt0_hosted_name = 'crt0_hosted' + target
84  crt0_minimal_name = 'crt0_minimal' + target
85  crt0_semihost_name = 'crt0_semihost' + target
86
87  # The normal variant does not call 'exit' after return from main (c lingo: freestanding execution environment)
88  _crt = executable(crt_name,
89		    src_picocrt,
90		    include_directories : inc,
91		    install : true,
92		    install_dir : instdir,
93		    c_args : value[1] + arg_fnobuiltin + ['-ffreestanding'],
94		    link_args : value[1] + ['-r', '-ffreestanding'])
95
96  set_variable(crt0_name,
97	       _crt.extract_objects(src_picocrt)
98	      )
99
100  if enable_picocrt_lib
101    static_library(libcrt_name,
102                   [],
103                   install : true,
104                   install_dir : instdir,
105                   pic: false,
106                   objects : [_crt.extract_objects(src_picocrt)])
107  endif
108
109  # The 'hosted' variant calls 'exit' after return from main (c lingo: hosted execution environment)
110  _crt = executable(crt_hosted_name,
111		    src_picocrt,
112		    include_directories : inc,
113		    install : true,
114		    install_dir : instdir,
115		    c_args : value[1] + arg_fnobuiltin + ['-ffreestanding', '-DCRT0_EXIT'],
116		    link_args : value[1] + ['-r', '-ffreestanding'])
117
118  set_variable(crt0_hosted_name,
119	       _crt.extract_objects(src_picocrt)
120	      )
121
122  if enable_picocrt_lib
123    static_library(libcrt_hosted_name,
124                   [],
125                   install : true,
126                   install_dir : instdir,
127                   pic: false,
128                   objects : [_crt.extract_objects(src_picocrt)])
129  endif
130
131  # The 'minimal' variant doesn't call exit, nor does it invoke any constructors
132  _crt = executable(crt_minimal_name,
133		    src_picocrt,
134		    include_directories : inc,
135		    install : true,
136		    install_dir : instdir,
137		    c_args : value[1] + arg_fnobuiltin + ['-ffreestanding', '-DCONSTRUCTORS=0'],
138		    link_args : value[1] + ['-r', '-ffreestanding'])
139
140  set_variable(crt0_minimal_name,
141	       _crt.extract_objects(src_picocrt)
142	      )
143
144  if enable_picocrt_lib
145    static_library(libcrt_minimal_name,
146                   [],
147                   install : true,
148                   install_dir : instdir,
149                   pic: false,
150                   objects : [_crt.extract_objects(src_picocrt)])
151  endif
152
153  if has_arm_semihost
154    # The 'semihost' variant calls sys_semihost_get_cmdline to build argv
155    # and calls exit when main returns
156    _crt = executable(crt_semihost_name,
157		      src_picocrt,
158		      include_directories : inc,
159		      install : true,
160		      install_dir : instdir,
161		      c_args : value[1] + arg_fnobuiltin + ['-ffreestanding', '-DCRT0_EXIT', '-DCRT0_SEMIHOST'],
162		      link_args : value[1] + ['-r', '-ffreestanding'])
163
164    set_variable(crt0_semihost_name,
165		 _crt.extract_objects(src_picocrt)
166		)
167
168    if enable_picocrt_lib
169      static_library(libcrt_semihost_name,
170                     [],
171                     install : true,
172                     install_dir : instdir,
173                     pic: false,
174                     objects : [_crt.extract_objects(src_picocrt)])
175    endif
176  endif
177endforeach
178