2018-12-29 15:40:56 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-01 08:54:58 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2018-12-29 15:40:56 +01:00
|
|
|
"fmt"
|
2021-04-17 20:33:01 +02:00
|
|
|
"io"
|
2018-12-29 15:40:56 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2019-05-19 20:42:50 +02:00
|
|
|
"sort"
|
2018-12-29 15:40:56 +01:00
|
|
|
"strings"
|
|
|
|
|
2020-08-13 17:44:04 +02:00
|
|
|
"github.com/hajimehoshi/file2byteslice"
|
2018-12-29 15:40:56 +01:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
type arch string
|
|
|
|
|
|
|
|
const (
|
|
|
|
archAmd64 arch = "amd64"
|
|
|
|
arch386 arch = "386"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a arch) gcc() string {
|
|
|
|
switch a {
|
|
|
|
case archAmd64:
|
|
|
|
return "x86_64-w64-mingw32-gcc"
|
|
|
|
case arch386:
|
|
|
|
return "i686-w64-mingw32-gcc"
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func c2o(str string) string {
|
|
|
|
return strings.TrimSuffix(filepath.Base(str), ".c") + ".o"
|
|
|
|
}
|
|
|
|
|
|
|
|
func execCommand(name string, args ...string) error {
|
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() error {
|
2019-05-19 20:48:11 +02:00
|
|
|
srcs, err := filepath.Glob(filepath.Join("glfw", "src", "*.c"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sort.Strings(srcs)
|
|
|
|
|
2018-12-29 15:40:56 +01:00
|
|
|
for _, arch := range []arch{archAmd64, arch386} {
|
|
|
|
g := errgroup.Group{}
|
|
|
|
for _, s := range srcs {
|
|
|
|
s := s
|
|
|
|
g.Go(func() error {
|
|
|
|
args := []string{
|
|
|
|
"-c",
|
|
|
|
"-o",
|
|
|
|
c2o(s),
|
|
|
|
"-D_GLFW_WIN32",
|
|
|
|
"-D_GLFW_BUILD_DLL",
|
|
|
|
"-Iglfw/deps/mingw",
|
|
|
|
"-g",
|
|
|
|
"-Os",
|
|
|
|
s,
|
|
|
|
}
|
|
|
|
if err := execCommand(arch.gcc(), args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
objs := []string{}
|
|
|
|
for _, s := range srcs {
|
|
|
|
objs = append(objs, c2o(s))
|
|
|
|
}
|
|
|
|
dll := fmt.Sprintf("glfw_windows_%s.dll", arch)
|
|
|
|
args := []string{
|
|
|
|
"-shared",
|
|
|
|
"-o",
|
|
|
|
dll,
|
|
|
|
"-Wl,--no-insert-timestamp",
|
|
|
|
}
|
|
|
|
args = append(args, objs...)
|
|
|
|
args = append(args, "-lopengl32", "-lgdi32")
|
|
|
|
if err := execCommand(arch.gcc(), args...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-17 20:33:01 +02:00
|
|
|
dllf, err := os.Open(dll)
|
2020-08-13 17:44:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-17 20:33:01 +02:00
|
|
|
defer dllf.Close()
|
|
|
|
|
|
|
|
hash := sha256.New()
|
|
|
|
in := io.TeeReader(dllf, hash)
|
2020-08-13 17:44:04 +02:00
|
|
|
|
|
|
|
out, err := os.Create(fmt.Sprintf("glfwdll_windows_%s.go", arch))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-12-29 15:40:56 +01:00
|
|
|
}
|
2020-08-13 17:44:04 +02:00
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// As the file name already specified the build environment, buildtags are not requried.
|
2021-04-17 20:33:01 +02:00
|
|
|
if err := file2byteslice.Write(out, in, true, "", "glfw", "glfwDLLCompressed"); err != nil {
|
2018-12-29 15:40:56 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-01 08:54:58 +01:00
|
|
|
// Dump the hash
|
|
|
|
hashout, err := os.Create(fmt.Sprintf("glfwdllhash_windows_%s.go", arch))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer hashout.Close()
|
2021-04-17 20:33:01 +02:00
|
|
|
|
|
|
|
hashsum := hash.Sum(nil)
|
2020-11-01 08:54:58 +01:00
|
|
|
if _, err := fmt.Fprintf(hashout, `// Code generated by gen.go. DO NOT EDIT.
|
|
|
|
|
|
|
|
package glfw
|
|
|
|
|
|
|
|
const glfwDLLHash = "%s"
|
2021-04-17 20:33:01 +02:00
|
|
|
`, hex.EncodeToString(hashsum[:])); err != nil {
|
2020-11-01 08:54:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the files.
|
2018-12-29 15:40:56 +01:00
|
|
|
for _, o := range objs {
|
|
|
|
if err := os.Remove(o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := os.Remove(dll); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := execCommand("gofmt", "-s", "-w", "."); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := run(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|