-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoGenGo.cmake
569 lines (496 loc) · 18 KB
/
AutoGenGo.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#----------------------------------------------------------------------
# Emit 'version.go', which contains system-specific constants.
# Based on the libgo Makefile recipe.
#
# Unnamed parameters:
#
# * output file
# * cmake binary directory for libfo
# * root of src containing libgo script files
#
function(mkversion outfile bindir srcroot scriptroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package sys\n")
# FIXME: typical LLVM usage allows for performing a build without
# CMAKE_INSTALL_PREFIX set, then doing an install from the build
# using a newly chosen CMAKE_INSTALL_PREFIX value. This mode is
# not currently supported -- the install prefix has to be set properly
# as part of the original build.
# FIXME:
# GccgoToolDir is set to the gccgo installation directory that contains
# (among other things) "go1", "cgo", "cc1", and other auxiliary
# executables. Where things like "cgo" will live hasn't been ironed
# out yet, so just pick a spot in the bin directory for now. See also
# 'DefaultGoRoot' above.
file(APPEND ${outfile} "const GccgoToolDir = \"${CMAKE_INSTALL_PREFIX}/tools\"\n")
file(APPEND ${outfile} "const StackGuardMultiplierDefault = 1\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'zgoos.go', which contains OS-specific constants.
# Based on the libgo Makefile recipe.
#
# Unnamed parameters:
#
# * GOOS setting (target OS)
# * output file
# * root of src containing libgo script files
#
function(mkzgoos goos outfile scriptroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package goos\n\n")
file(APPEND ${outfile} "const GOOS = \"${goos}\"\n\n")
foreach (os ${allgoos})
set(val "0")
if( ${os} STREQUAL ${goos})
set(val "1")
endif()
upperfirst(${os} "upos")
file(APPEND ${outfile} "const Is${upos} = ${val}\n")
endforeach()
endfunction()
#----------------------------------------------------------------------
# Emit 'zgoarch.go', which contains ARCH-specific constants.
# Based on the libgo Makefile recipe.
#
# Unnamed parameters:
#
# * GOARCH setting (target arch)
# * output file
# * root of src containing libgo script files
#
function(mkzgoarch goarch outfile scriptroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package goarch\n\n")
file(APPEND ${outfile} "const GOARCH = \"${goarch}\"\n\n")
set(constants "ArchFamily:family" "BigEndian:bigendian" "DefaultPhysPageSize:defaultphyspagesize" "Int64Align:int64align" "MinFrameSize:minframesize" "PCQuantum:pcquantum" "StackAlign:stackalign")
file(APPEND ${outfile} "const (\n")
foreach(item ${constants})
# Split item into Go constant name and
string(REGEX REPLACE ":" " " sitem ${item})
separate_arguments(sitem)
list(GET sitem 0 constname)
list(GET sitem 1 scriptarg)
# Invoke goarch.sh
execute_process(COMMAND ${shell} "${scriptroot}/goarch.sh"
${goarch} ${scriptarg}
OUTPUT_VARIABLE result
ERROR_VARIABLE errmsg
RESULT_VARIABLE exitstatus)
if(${exitstatus} MATCHES 0)
file(APPEND ${outfile} "\t_${constname} = ${result}")
else()
message(FATAL_ERROR "goarch.sh invocation failed: ${errmsg}")
endif()
endforeach()
file(APPEND ${outfile} ")\n")
file(APPEND ${outfile} "\n")
file(APPEND ${outfile} "const (\n")
file(APPEND ${outfile} "\tUNKNOWN ArchFamilyType = iota\n")
foreach (af ${allgoarchfamily})
file(APPEND ${outfile} "\t${af}\n")
endforeach()
file(APPEND ${outfile} ")\n\n")
foreach (arch ${allgoarch})
set(val "0")
if( ${arch} STREQUAL ${goarch})
set(val "1")
endif()
upperfirst(${arch} "uparch")
file(APPEND ${outfile} "const Is${uparch} = ${val}\n")
endforeach()
file(APPEND ${outfile} "\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'cpugen.go'. Similar to version.go, but with a smaller set of
# CPU-specific constants. Based on the libgo Makefile recipe.
#
# Unnamed parameters:
#
# * GOARCH setting (target architecture)
# * output file
# * root of src containing libgo script files
#
function(mkcpugen goarch outfile scriptroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package cpu\n")
# Invoke goarch.sh
execute_process(COMMAND ${shell} "${scriptroot}/goarch.sh"
${goarch} "cachelinesize"
OUTPUT_VARIABLE result
ERROR_VARIABLE errmsg
RESULT_VARIABLE exitstatus)
if(${exitstatus} MATCHES 0)
file(APPEND ${outfile} "const CacheLinePadSize = ${result}\n")
else()
message(FATAL_ERROR "goarch.sh invocation failed: ${errmsg}")
endif()
# For now this is hard-wired off. If/when gollvm supports
# PPC64 ELF ABI v1 we'll need to configure it.
file(APPEND ${outfile} "const FunctionDescriptors = false\n")
file(APPEND ${outfile} "\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'gcpugen.go', another cpu-specific generated Go file. Based
# on the libgo Makefile recipe.
#
# Unnamed parameters:
#
# * GOARCH setting (target architecture)
# * output file
# * root of src containing libgo script files
#
function(mkgcpugen goarch outfile scriptroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package cpu\n")
# Invoke goarch.sh
execute_process(COMMAND ${shell} "${scriptroot}/goarch.sh"
${goarch} "cachelinesize"
OUTPUT_VARIABLE result
ERROR_VARIABLE errmsg
RESULT_VARIABLE exitstatus)
if(${exitstatus} MATCHES 0)
file(APPEND ${outfile} "const cacheLineSize = ${result}\n")
else()
message(FATAL_ERROR "goarch.sh invocation failed: ${errmsg}")
endif()
file(APPEND ${outfile} "\n")
endfunction()
macro(upperfirst name result)
string(SUBSTRING ${name} 0 1 c1)
string(SUBSTRING ${name} 1 -1 crem)
string(TOUPPER ${c1} upc1)
set(${result} "${upc1}${crem}")
endmacro()
#----------------------------------------------------------------------
# Emit compiler version string to specified output file. This is extracted
# out into a separate function since it is needed in a couple of different
# places.
#
# Unnamed parameters:
#
# * output file to target
# * root of libgo source
function(emitversionstring outfile srcroot)
file(STRINGS "${srcroot}/../VERSION" rawver)
string(STRIP ${rawver} ver)
file(APPEND ${outfile} "\"${ver} gollvm LLVM ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX}\"")
endfunction()
#----------------------------------------------------------------------
# Emit 'gccgosizes.go', which includes size and alignment constants
# by architecture.
#
# Unnamed parameters:
#
# * GOARCH setting for target
# * output file to target
# * directory containing libgo scripts
#
function(mkgccgosizes goarch outfile scriptroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package types\n\n")
file(APPEND ${outfile} "var gccgoArchSizes = map[string]*StdSizes{\n")
foreach(arch ${allgoarch})
execute_process(COMMAND ${shell} "${scriptroot}/goarch.sh"
${arch} "ptrsize"
OUTPUT_VARIABLE presult
ERROR_VARIABLE errmsg
RESULT_VARIABLE exitstatus)
if(NOT ${exitstatus} MATCHES 0)
message(FATAL_ERROR "goarch.sh invocation failed: ${errmsg}")
endif()
execute_process(COMMAND ${shell} "${scriptroot}/goarch.sh"
${arch} "maxalign"
OUTPUT_VARIABLE aresult
ERROR_VARIABLE errmsg
RESULT_VARIABLE exitstatus)
if(NOT ${exitstatus} MATCHES 0)
message(FATAL_ERROR "goarch.sh invocation failed: ${errmsg}")
endif()
string(STRIP ${presult} presult)
string(STRIP ${aresult} aresult)
file(APPEND ${outfile} "\"${arch}\": {${presult}, ${aresult}},\n")
endforeach()
file(APPEND ${outfile} "}\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'buildcfg.go', containing default settings for various GO* vars.
#
# Unnamed parameters:
#
# * output file to target
# * libgo cmake binary directory
# * libgo source code root directory
#
function(mkbuildcfg outfile binroot srcroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package buildcfg\n\n")
file(APPEND ${outfile} "import \"runtime\"\n")
# Compiler version
file(STRINGS "${srcroot}/../VERSION" rawver)
string(STRIP ${rawver} ver)
file(APPEND ${outfile} "const version = ")
emitversionstring(${outfile} ${srcroot})
file(APPEND ${outfile} "\n")
file(APPEND ${outfile} "func defaultGOROOTValue() string { return \"${GOLLVM_INSTALL_DIR}\" }\n")
file(APPEND ${outfile} "const defaultGO386 = `sse2`\n")
file(APPEND ${outfile} "const defaultGOAMD64 = `v1`\n")
file(APPEND ${outfile} "const defaultGOARM = `5`\n")
file(APPEND ${outfile} "const defaultGOMIPS = `hardfloat`\n")
file(APPEND ${outfile} "const defaultGOMIPS64 = `hardfloat`\n")
file(APPEND ${outfile} "const defaultGOOS = runtime.GOOS\n")
file(APPEND ${outfile} "const defaultGOPPC64 = `power8`\n")
file(APPEND ${outfile} "const defaultGOARCH = runtime.GOARCH\n")
file(APPEND ${outfile} "const defaultGO_EXTLINK_ENABLED = ``\n")
file(APPEND ${outfile} "const defaultGO_LDSO = ``\n")
file(APPEND ${outfile} "const defaultGOEXPERIMENT = `fieldtrack`\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'objabi.go', containing the stack guard multiplier constant.
#
# Unnamed parameters:
#
# * output file to target
# * libgo cmake binary directory
# * libgo source code root directory
#
function(mkobjabi outfile binroot srcroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package objabi\n\n")
file(APPEND ${outfile} "const stackGuardMultiplierDefault = 1\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'goroot.go', containing the default GOROOT setting.
#
# Unnamed parameters:
#
# * output file to target
# * libgo cmake binary directory
# * libgo source code root directory
#
function(mkgoroot outfile binroot srcroot)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package runtime\n\n")
file(APPEND ${outfile} "var defaultGOROOT = \"${GOLLVM_INSTALL_DIR}\"\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'zstdpkglist.go', containing a map with all of the std Go packages.
#
# Unnamed parameters:
#
# * package to use for generated Go code
# * output file to target
# * list of go std library packages (does not include libgotool packages)
#
function(mkzstdpkglist package outfile libpackages)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package ${package}\n\n")
file(APPEND ${outfile} "var stdpkg = map[string]bool{")
foreach(pack ${libpackages})
string(REGEX MATCH "^.*golang\.org\/.*$" matched ${pack})
if(NOT matched)
file(APPEND ${outfile} "\"${pack}\": true,\n")
endif()
endforeach()
file(APPEND ${outfile} "\"unsafe\": true,\n")
file(APPEND ${outfile} "}\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'zdefaultcc.go', containing info on where to find C/C++ compilers.
#
# Unnamed parameters:
#
# * package to use for generated Go code
# * output file to target
# * C compiler path
# * C++ compiler path
#
# Named parameters:
#
# EXPORT Generated public functions (ex: DefaultCC not defaultCC).
#
function(mkzdefaultcc package outfile ccpath cxxpath)
CMAKE_PARSE_ARGUMENTS(ARG "EXPORT" "" "" ${ARGN})
# Construct default driver path
set(driverpath "${GOLLVM_INSTALL_DIR}/bin/llvm-goc")
file(REMOVE ${outfile})
file(WRITE ${outfile} "package ${package}\n\n")
set(f1 "defaultGCCGO")
set(f2 "defaultCC")
set(f3 "defaultCXX")
set(f4 "defaultPkgConfig")
set(v1 "oSArchSupportsCgo")
if( ${ARG_EXPORT} )
upperfirst(${f1} "f1")
upperfirst(${f2} "f2")
upperfirst(${f3} "f3")
upperfirst(${f4} "f4")
upperfirst(${v1} "v1")
endif()
file(APPEND ${outfile} "func ${f1}(goos, goarch string) string { return \"${driverpath}\" }\n")
file(APPEND ${outfile} "func ${f2}(goos, goarch string) string { return \"${ccpath}\" }\n")
file(APPEND ${outfile} "func ${f3}(goos, goarch string) string { return \"${cxxpath}\" }\n")
file(APPEND ${outfile} "const ${f4} = \"pkg-config\"\n")
file(APPEND ${outfile} "var ${v1} = map[string]bool{}\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'epoll.go', containing info on the epoll syscall. Reads
# variables SIZEOF_STRUCT_EPOLL_EVENT and STRUCT_EPOLL_EVENT_FD_OFFSET
# previously set by config process.
#
# Unnamed parameters:
#
# * output file to target
#
function(mkepoll outfile)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package syscall\n\n")
set(s ${SIZEOF_STRUCT_EPOLL_EVENT})
set(o ${STRUCT_EPOLL_EVENT_FD_OFFSET})
file(APPEND ${outfile} "type EpollEvent struct {")
file(APPEND ${outfile} " Events uint32\n")
if(${s} EQUAL 0 AND ${o} EQUAL 0)
message(SEND_ERROR "*** struct epoll_event data.fd offset unknown")
elseif(${s} EQUAL 8 AND ${o} EQUAL 4)
file(APPEND ${outfile} " Fd int32\n")
elseif(${s} EQUAL 12 AND ${o} EQUAL 4)
file(APPEND ${outfile} " Fd int32\n Pad [4]byte\n")
elseif(${s} EQUAL 12 AND ${o} EQUAL 8)
file(APPEND ${outfile} " Pad [4]byte\n Fd int32\n")
elseif(${s} EQUAL 16 AND ${o} EQUAL 8)
file(APPEND ${outfile} " Pad [4]byte\n Fd int32\n Pad2 [4]byte\n ")
else()
message(SEND_ERROR "*** struct epoll_event data.fd offset unknown")
endif()
file(APPEND ${outfile} "}\n")
endfunction()
#----------------------------------------------------------------------
# Emit 'syscall_arch.go', containing arch and os constants.
#
# Unnamed parameters:
#
# * output file to target
# * GOOS setting (target OS)
# * GOARCH setting (target architecture)
#
function(mksyscallarch outfile goos goarch)
file(REMOVE ${outfile})
file(WRITE ${outfile} "package syscall\n\n")
file(APPEND ${outfile} "const ARCH = \"${goarch}\"\n")
file(APPEND ${outfile} "const OS = \"${goos}\"\n")
endfunction()
# This helper function provides a general mechanism for running a
# shell script to emit a Go code to a temp file, then adds a cmake
# target that copies the temp to a specific *.go file if the temp
# is different.
#
# Example usage:
#
# generate_go_from_script(outfile script goos goarch workdir DEP <deps>)
#
# Unnamed parameters:
#
# * generated Go source file (this should be a full path)
# * script to run
# * GOOS setting (target OS)
# * GOARCH setting (target architecture)
# * working bin directory
#
# Named parameters:
#
# CAPTURE Capture stdout from script into output file.
# SCRIPTARGS Additional args to pass to script.
# DEP Things that the generated file should depend on.
#
function(generate_go_from_script outpath script goos goarch workdir)
CMAKE_PARSE_ARGUMENTS(ARG "CAPTURE" "" "DEP;SCRIPTARGS" ${ARGN})
get_filename_component(outfile "${outpath}" NAME)
get_filename_component(outdir "${outpath}" DIRECTORY)
set(tmpfile "${outdir}/tmp-${outfile}")
if(DEFINED ENV{SHELL})
set(shell $ENV{SHELL})
else()
set(shell "/bin/bash")
endif()
# Create a rule that runs the script into a temporary file.
if( NOT ARG_CAPTURE )
add_custom_command(
# For this variant, the assumption is that the script writes it
# output to a specified file.
OUTPUT ${tmpfile}
COMMAND "GOARCH=${goarch}" "GOOS=${goos}"
"${shell}" ${script} ${ARG_SCRIPTARGS}
COMMENT "Creating ${tmpfile}"
DEPENDS ${script} ${ARG_DEP}
WORKING_DIRECTORY ${workdir}
VERBATIM)
else()
# For this variant, the script is emitting output to stdout,
# which we need to capture and redirect to a file.
set(capturesh "${CMAKE_CURRENT_SOURCE_DIR}/capturescript.sh")
add_custom_command(
OUTPUT ${tmpfile}
COMMAND "GOARCH=${goarch}" "GOOS=${goos}"
"${shell}" ${capturesh} ${script} ${tmpfile} ${ARG_SCRIPTARGS}
COMMENT "Creating ${tmpfile}"
DEPENDS ${script} ${ARG_DEP}
WORKING_DIRECTORY ${workdir}
VERBATIM)
endif()
# Create a rule that copies tmp file to output file if different.
copy_if_different(${tmpfile} ${outpath})
endfunction()
# This function manages the cmake rules for the auto-generated
# 'gen-sysinfo.go' file, which contains type information derived from
# compiling a C program that includes various system headers.
#
# Unnamed parameters:
#
# * name of tmpfile into which to generate provisional Go code
# * name of generated Go source file for final Go code
# * name of macro temp file to generate
# * name of temp object file to write
# * path to godumpspec tool
# * path to sysinfo.c
#
# Names params:
#
# * DEPS -- things gen-sysinfo.go is dependent on (ex: config.h)
# * CFLAGS -- additional compile options (ex: -I ...)
#
function(mkgensysinfo tmpfile outfile macrofile objfile godumpspec sysinfoc)
CMAKE_PARSE_ARGUMENTS(ARG "" "" "DEPS;CFLAGS" ${ARGN})
set(ccomp ${CMAKE_C_COMPILER})
set(cflags ${ARG_CFLAGS})
if(NOT "${CMAKE_SYSROOT}" STREQUAL "")
set(cflags ${cflags} "--sysroot=${CMAKE_SYSROOT}")
endif()
# Run the host C compiler to generate the object. NB: clang will
# accept -fno-eliminate-unused-debug-types but does not actually
# implement this functionality.
add_custom_command(
OUTPUT ${objfile}
COMMAND ${ccomp} "-g2" "-c" "-fno-eliminate-unused-debug-types"
${sysinfoc} -o ${objfile} ${cflags}
DEPENDS ${sysinfoc} ${ARG_DEPS}
COMMENT "Building sysinfo.o "
VERBATIM)
# Another compile to build the macro temp file.
add_custom_command(
OUTPUT ${macrofile}
COMMAND ${ccomp} "-E" "-dM"
${sysinfoc} -o ${macrofile} ${cflags}
DEPENDS ${sysinfoc} ${ARG_DEPS}
COMMENT "Building sysinfo.c macro temp file"
VERBATIM)
# Next step is to run the llvm-godumpspec utility, which consumes
# the two files produced above and generates the Go file in question.
add_custom_command(
OUTPUT ${tmpfile}
COMMAND ${godumpspec}
"--macrotmp=${macrofile}" "--object=${objfile}" "--output=${tmpfile}"
DEPENDS ${objfile} ${macrofile} ${ARG_DEPS}
COMMENT "Generating ${outfile}"
VERBATIM)
# Create a rule that copies tmpfile to output file if different.
copy_if_different(${tmpfile} ${outfile})
endfunction()