-
Notifications
You must be signed in to change notification settings - Fork 24
/
fltk-build.go
258 lines (229 loc) · 8.61 KB
/
fltk-build.go
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
//go:build ignore
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
const commit = "d547e1956778ed584248179d9141843b86101a0a"
func main() {
if runtime.GOOS == "" {
fmt.Println("GOOS environment variable is empty")
os.Exit(1)
}
if runtime.GOARCH == "" {
fmt.Println("GOARCH environment variable is empty")
os.Exit(1)
}
fmt.Printf("Building FLTK for OS: %s, architecture: %s\n", runtime.GOOS, runtime.GOARCH)
if _, err := exec.LookPath("git"); err != nil {
fmt.Printf("Cannot find git binary, %v\n", err)
os.Exit(1)
}
if _, err := exec.LookPath("cmake"); err != nil {
fmt.Printf("Cannot find cmake binary, %v\n", err)
os.Exit(1)
}
libDir := filepath.Join("lib", runtime.GOOS, runtime.GOARCH)
if err := os.MkdirAll(libDir, 0750); err != nil {
fmt.Printf("Could not create directory %s, %v\n", libDir, err)
os.Exit(1)
}
includeDir := filepath.Join("include", runtime.GOOS, runtime.GOARCH)
if err := os.MkdirAll(includeDir, 0750); err != nil {
fmt.Printf("Could not create directory %s, %v\n", includeDir, err)
os.Exit(1)
}
if err := os.MkdirAll("fltk_build", 0750); err != nil {
fmt.Printf("Could not create directory fltk_build, %v\n", err)
os.Exit(1)
}
currentDir, err := os.Getwd()
if err != nil {
fmt.Printf("Cannot get current directory, %v\n", err)
os.Exit(1)
}
fltkSourceDir := filepath.Join("fltk_build", "fltk")
fltkStat, err := os.Stat(fltkSourceDir)
if errors.Is(err, fs.ErrNotExist) {
fmt.Println("Cloning FLTK repository")
cloneCmd := exec.Command("git", "clone", "https://github.com/fltk/fltk.git")
cloneCmd.Dir = "fltk_build"
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr
if err := cloneCmd.Run(); err != nil {
fmt.Printf("Error cloning FLTK repository, %v\n", err)
os.Exit(1)
}
} else if fltkStat.IsDir() {
fmt.Println("Found existing FLTK directory")
if runtime.GOOS == "windows" {
checkoutCmd := exec.Command("git", "checkout", "src/Fl_win32.cxx")
checkoutCmd.Dir = fltkSourceDir
checkoutCmd.Stdout = os.Stdout
checkoutCmd.Stderr = os.Stderr
if err := checkoutCmd.Run(); err != nil {
fmt.Printf("Error checking out Fl_win32.cxx, %v\n", err)
os.Exit(1)
}
}
fetchCmd := exec.Command("git", "fetch")
fetchCmd.Dir = fltkSourceDir
fetchCmd.Stdout = os.Stdout
fetchCmd.Stderr = os.Stderr
if err := fetchCmd.Run(); err != nil {
fmt.Printf("Error fetching FLTK source, %v\n", err)
os.Exit(1)
}
} else {
fmt.Printf("Location for FLTK source code, %s, is not directory\n", fltkSourceDir)
os.Exit(1)
}
checkoutCmd := exec.Command("git", "checkout", commit)
checkoutCmd.Dir = fltkSourceDir
checkoutCmd.Stdout = os.Stdout
checkoutCmd.Stderr = os.Stderr
if err := checkoutCmd.Run(); err != nil {
fmt.Printf("Error checking out FLTK source")
os.Exit(1)
}
if runtime.GOOS == "windows" {
applyCmd := exec.Command("git", "apply", "../../lib/fltk-1.4.patch")
applyCmd.Dir = fltkSourceDir
applyCmd.Stdout = os.Stdout
applyCmd.Stderr = os.Stderr
if err := applyCmd.Run(); err != nil {
fmt.Printf("Error applying patch, %v\n", err)
os.Exit(1)
}
}
var cmakeGenerator string
if runtime.GOOS == "windows" {
cmakeGenerator = "MinGW Makefiles"
} else {
cmakeGenerator = "Unix Makefiles"
}
cmakeCmd := exec.Command(
"cmake", "-G", cmakeGenerator, "-S", "fltk", "-B", "build",
"-DCMAKE_BUILD_TYPE=Release",
"-DFLTK_BUILD_TEST=OFF",
"-DFLTK_BUILD_EXAMPLES=OFF",
"-DFLTK_BUILD_FLUID=OFF",
"-DFLTK_BUILD_HTML_DOCS=OFF",
"-DFLTK_BUILD_PDF_DOCS=OFF",
"-DFLTK_BUILD_FLTK_OPTIONS=OFF",
"-DFLTK_USE_SYSTEM_LIBJPEG=OFF",
"-DFLTK_USE_SYSTEM_LIBPNG=OFF",
"-DFLTK_USE_SYSTEM_ZLIB=OFF",
"-DCMAKE_INSTALL_PREFIX="+currentDir,
"-DCMAKE_INSTALL_INCLUDEDIR="+includeDir,
"-DCMAKE_INSTALL_LIBDIR="+libDir,
"-DFLTK_INCLUDEDIR="+filepath.Join(currentDir, "include", runtime.GOOS, runtime.GOARCH),
"-DFLTK_LIBDIR="+filepath.Join(currentDir, "lib", runtime.GOOS, runtime.GOARCH))
if runtime.GOOS == "darwin" {
if runtime.GOARCH == "amd64" {
cmakeCmd.Args = append(cmakeCmd.Args, "-DCMAKE_OSX_ARCHITECTURES=x86_64")
} else if runtime.GOARCH == "arm64" {
cmakeCmd.Args = append(cmakeCmd.Args, "-DCMAKE_OSX_ARCHITECTURES=arm64")
} else {
fmt.Printf("Unsupported MacOS architecture, %s\n", runtime.GOARCH)
os.Exit(1)
}
}
cmakeCmd.Dir = "fltk_build"
cmakeCmd.Stdout = os.Stdout
cmakeCmd.Stderr = os.Stderr
if err := cmakeCmd.Run(); err != nil {
fmt.Printf("Error running cmake, %v\n", err)
os.Exit(1)
}
cmakeBuildArgs := []string{"--build", "build", "--parallel"}
if runtime.GOOS == "openbsd" {
cmakeBuildArgs = []string{"--build", "build"}
}
cmakeBuildCmd := exec.Command("cmake", cmakeBuildArgs...)
cmakeBuildCmd.Dir = "fltk_build"
cmakeBuildCmd.Stdout = os.Stdout
cmakeBuildCmd.Stderr = os.Stderr
if err := cmakeBuildCmd.Run(); err != nil {
fmt.Printf("Error running cmake build, %v\n", err)
os.Exit(1)
}
cmakeInstallCmd := exec.Command("cmake", "--install", "build")
cmakeInstallCmd.Dir = "fltk_build"
cmakeInstallCmd.Stdout = os.Stdout
cmakeInstallCmd.Stderr = os.Stderr
if err := cmakeInstallCmd.Run(); err != nil {
fmt.Printf("Error running cmake install, %v\n", err)
os.Exit(1)
}
cgoFilename := "cgo_" + runtime.GOOS + "_" + runtime.GOARCH + ".go"
cgoFile, err := os.Create(cgoFilename)
if err != nil {
fmt.Printf("Error opening cgo file, %s, for writing, %v\n", cgoFilename, err)
os.Exit(1)
}
defer cgoFile.Close()
fmt.Fprintf(cgoFile, "//go:build %s && %s\n\n", runtime.GOOS, runtime.GOARCH)
fmt.Fprintln(cgoFile, "package fltk\n")
fmt.Fprintf(cgoFile, "// #cgo %s,%s CXXFLAGS: -std=c++11\n", runtime.GOOS, runtime.GOARCH)
if runtime.GOOS != "windows" {
fltkConfigPath := filepath.Join("fltk_build", "build", "bin", "fltk-config")
fltkConfigStat, err := os.Stat(fltkConfigPath)
if err != nil {
fmt.Printf("Error checking for fltk-config file, %v\n", err)
os.Exit(1)
}
if !fltkConfigStat.Mode().IsRegular() {
fmt.Println("fltk-config file is not a regular file")
os.Exit(1)
}
if err := os.Chmod(fltkConfigPath, fltkConfigStat.Mode().Perm()|0111); err != nil {
fmt.Printf("Error making fltk-config executable, %v", err)
os.Exit(1)
}
fltkConfigCxxCmd := exec.Command(fltkConfigPath, "--use-gl", "--use-images", "--use-forms", "--cxxflags")
cxxOutput, err := fltkConfigCxxCmd.Output()
if err != nil {
fmt.Printf("Error running fltk-config --cxxflags, %v\n", err)
os.Exit(1)
}
fltkConfigCxxFlags := strings.ReplaceAll(string(cxxOutput), currentDir, "${SRCDIR}")
if runtime.GOOS == "openbsd" {
fltkConfigCxxFlags = "-I/usr/X11R6/include " + fltkConfigCxxFlags
}
fmt.Fprintf(cgoFile, "// #cgo %s,%s CPPFLAGS: %s", runtime.GOOS, runtime.GOARCH, fltkConfigCxxFlags)
if fltkConfigCxxFlags[len(fltkConfigCxxFlags)-1] != '\n' {
fmt.Fprintln(cgoFile, "")
}
fltkConfigLdCmd := exec.Command(fltkConfigPath, "--use-gl", "--use-images", "--use-forms", "--ldstaticflags")
ldOutput, err := fltkConfigLdCmd.Output()
if err != nil {
fmt.Printf("Error running fltk-config --ldstaticflags, %v\n", err)
os.Exit(1)
}
fltkConfigLdFlags := strings.ReplaceAll(string(ldOutput), currentDir, "${SRCDIR}")
if runtime.GOOS == "openbsd" {
fltkConfigLdFlags = "-L/usr/X11R6/lib " + fltkConfigLdFlags
}
fmt.Fprintf(cgoFile, "// #cgo %s,%s LDFLAGS: %s", runtime.GOOS, runtime.GOARCH, fltkConfigLdFlags)
if fltkConfigLdFlags[len(fltkConfigLdFlags)-1] != '\n' {
fmt.Fprintln(cgoFile, "")
}
} else {
// Switching to slashes in paths in cgo directives as backslashes are causing problems when being passed to gcc.
libDirWithSlashes := filepath.ToSlash(libDir)
includeDirWithSlashes := filepath.ToSlash(includeDir)
// Hardcoding contents of cgo directive for windows,
// as we cannot extract it from fltk-config if we're not using a UNIX shell.
fmt.Fprintf(cgoFile, "// #cgo %s,%s CPPFLAGS: -I${SRCDIR}/%s -I${SRCDIR}/%s/FL/images -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64\n", runtime.GOOS, runtime.GOARCH, includeDirWithSlashes, includeDirWithSlashes)
fmt.Fprintf(cgoFile, "// #cgo %s,%s LDFLAGS: -mwindows ${SRCDIR}/%s/libfltk_images.a ${SRCDIR}/%s/libfltk_jpeg.a ${SRCDIR}/%s/libfltk_png.a ${SRCDIR}/%s/libfltk_z.a ${SRCDIR}/%s/libfltk_gl.a -lglu32 -lopengl32 ${SRCDIR}/%s/libfltk_forms.a ${SRCDIR}/%s/libfltk.a -lgdiplus -lole32 -luuid -lcomctl32 -lws2_32 -lwinspool\n", runtime.GOOS, runtime.GOARCH, libDirWithSlashes, libDirWithSlashes, libDirWithSlashes, libDirWithSlashes, libDirWithSlashes, libDirWithSlashes, libDirWithSlashes)
}
fmt.Fprintln(cgoFile, "import \"C\"")
fmt.Printf("Successfully generated libraries for OS: %s, architecture: %s\n", runtime.GOOS, runtime.GOARCH)
}