Skip to content

Commit 1e7668b

Browse files
author
jld3103
authored
Merge pull request #147 from go-flutter-desktop/feature/aot
Add AOT building
2 parents 4bd6c08 + 9ad26f5 commit 1e7668b

File tree

10 files changed

+402
-264
lines changed

10 files changed

+402
-264
lines changed

cmd/build.go

Lines changed: 253 additions & 80 deletions
Large diffs are not rendered by default.

cmd/bumpversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var upgradeCmd = &cobra.Command{
3636
}
3737

3838
func upgrade(targetOS string) (err error) {
39-
enginecache.ValidateOrUpdateEngine(targetOS, buildOrRunCachePath, "")
39+
enginecache.ValidateOrUpdateEngine(targetOS, buildOrRunCachePath, "", build.DebugMode)
4040
return upgradeGoFlutter(targetOS)
4141
}
4242

cmd/docker.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
)
1717

1818
func dockerHoverBuild(targetOS string, packagingTask packaging.Task, buildFlags []string, vmArguments []string) {
19-
initBuildParameters(targetOS)
2019
var err error
2120
dockerBin := build.DockerBin()
2221

cmd/packaging/noop.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package packaging
22

3+
import "github.com/go-flutter-desktop/hover/internal/build"
4+
35
type noopTask struct{}
46

57
var NoopTask Task = &noopTask{}
68

7-
func (_ *noopTask) Name() string { return "" }
8-
func (_ *noopTask) Init() {}
9-
func (_ *noopTask) IsInitialized() bool { return true }
10-
func (_ *noopTask) AssertInitialized() {}
11-
func (_ *noopTask) Pack(string) {}
12-
func (_ *noopTask) AssertSupported() {}
9+
func (_ *noopTask) Name() string { return "" }
10+
func (_ *noopTask) Init() {}
11+
func (_ *noopTask) IsInitialized() bool { return true }
12+
func (_ *noopTask) AssertInitialized() {}
13+
func (_ *noopTask) Pack(string, build.Mode) {}
14+
func (_ *noopTask) AssertSupported() {}

cmd/packaging/packaging.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (t *packagingTask) init(ignoreAlreadyExists bool) {
128128
}
129129
}
130130

131-
func (t *packagingTask) Pack(fullVersion string) {
131+
func (t *packagingTask) Pack(fullVersion string, mode build.Mode) {
132132
projectName := pubspec.GetPubSpec().Name
133133
version := strings.Split(fullVersion, "+")[0]
134134
var release string
@@ -137,7 +137,7 @@ func (t *packagingTask) Pack(fullVersion string) {
137137
} else {
138138
release = strings.ReplaceAll(fullVersion, ".", "")
139139
}
140-
description := pubspec.GetPubSpec().Description
140+
description := pubspec.GetPubSpec().GetDescription()
141141
organizationName := androidmanifest.AndroidOrganizationName()
142142
author := pubspec.GetPubSpec().GetAuthor()
143143
applicationName := config.GetConfig().GetApplicationName(projectName)
@@ -158,17 +158,17 @@ func (t *packagingTask) Pack(fullVersion string) {
158158
}
159159
templateData["iconPath"] = executeStringTemplate(t.linuxDesktopFileIconPath, templateData)
160160
templateData["executablePath"] = executeStringTemplate(t.linuxDesktopFileExecutablePath, templateData)
161-
t.pack(templateData, packageName, projectName, applicationName, executableName, version, release)
161+
t.pack(templateData, packageName, projectName, applicationName, executableName, version, release, mode)
162162
}
163163

164-
func (t *packagingTask) pack(templateData map[string]string, packageName, projectName, applicationName, executableName, version, release string) {
164+
func (t *packagingTask) pack(templateData map[string]string, packageName, projectName, applicationName, executableName, version, release string, mode build.Mode) {
165165
if t.extraTemplateData != nil {
166166
for key, value := range t.extraTemplateData(packageName, packagingFormatPath(t.packagingFormatName)) {
167167
templateData[key] = value
168168
}
169169
}
170170
for task := range t.dependsOn {
171-
task.pack(templateData, packageName, projectName, applicationName, executableName, version, release)
171+
task.pack(templateData, packageName, projectName, applicationName, executableName, version, release, mode)
172172
}
173173
tmpPath := getTemporaryBuildDirectory(projectName, t.packagingFormatName)
174174
defer func() {
@@ -181,14 +181,14 @@ func (t *packagingTask) pack(templateData map[string]string, packageName, projec
181181
log.Infof("Packaging %s in %s", strings.Split(t.packagingFormatName, "-")[1], tmpPath)
182182

183183
if t.flutterBuildOutputDirectory != "" {
184-
err := copy.Copy(build.OutputDirectoryPath(strings.Split(t.packagingFormatName, "-")[0]), executeStringTemplate(filepath.Join(tmpPath, t.flutterBuildOutputDirectory), templateData))
184+
err := copy.Copy(build.OutputDirectoryPath(strings.Split(t.packagingFormatName, "-")[0], mode), executeStringTemplate(filepath.Join(tmpPath, t.flutterBuildOutputDirectory), templateData))
185185
if err != nil {
186186
log.Errorf("Could not copy build folder: %v", err)
187187
os.Exit(1)
188188
}
189189
}
190190
for task, destination := range t.dependsOn {
191-
err := copy.Copy(build.OutputDirectoryPath(task.packagingFormatName), filepath.Join(tmpPath, destination))
191+
err := copy.Copy(build.OutputDirectoryPath(task.packagingFormatName, mode), filepath.Join(tmpPath, destination))
192192
if err != nil {
193193
log.Errorf("Could not copy build folder of %s: %v", task.packagingFormatName, err)
194194
os.Exit(1)
@@ -208,10 +208,10 @@ func (t *packagingTask) pack(templateData map[string]string, packageName, projec
208208
}
209209
}
210210

211-
err := os.RemoveAll(build.OutputDirectoryPath(t.packagingFormatName))
211+
err := os.RemoveAll(build.OutputDirectoryPath(t.packagingFormatName, mode))
212212
log.Printf("Cleaning the build directory")
213213
if err != nil {
214-
log.Errorf("Failed to clean output directory %s: %v", build.OutputDirectoryPath(t.packagingFormatName), err)
214+
log.Errorf("Failed to clean output directory %s: %v", build.OutputDirectoryPath(t.packagingFormatName, mode), err)
215215
os.Exit(1)
216216
}
217217

@@ -225,7 +225,7 @@ func (t *packagingTask) pack(templateData map[string]string, packageName, projec
225225
os.Exit(1)
226226
}
227227
outputFileName := filepath.Base(relativeOutputFilePath)
228-
outputFilePath := filepath.Join(build.OutputDirectoryPath(t.packagingFormatName), outputFileName)
228+
outputFilePath := filepath.Join(build.OutputDirectoryPath(t.packagingFormatName, mode), outputFileName)
229229
err = copy.Copy(filepath.Join(tmpPath, relativeOutputFilePath), outputFilePath)
230230
if err != nil {
231231
log.Errorf("Could not move %s file: %v", outputFileName, err)

cmd/packaging/task.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package packaging
22

3+
import "github.com/go-flutter-desktop/hover/internal/build"
4+
35
// Task contains all configuration options for a given packaging method.
46
// TODO: Rename to something that suits it more? Mabe Executor?
57
type Task interface {
68
Name() string
79
Init()
810
IsInitialized() bool
911
AssertInitialized()
10-
Pack(buildVersion string)
12+
Pack(buildVersion string, mode build.Mode)
1113
AssertSupported()
1214
}

cmd/run.go

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"os"
88
"os/exec"
9-
"path/filepath"
109
"regexp"
1110
"runtime"
1211

@@ -20,19 +19,15 @@ import (
2019
)
2120

2221
var (
23-
runObservatoryPort string
24-
runInitialRoute string
25-
runOmitEmbedder bool
26-
runOmitFlutterBundle bool
22+
runObservatoryPort string
23+
runInitialRoute string
2724
)
2825

2926
func init() {
3027
initCompileFlags(runCmd)
3128

3229
runCmd.Flags().StringVar(&runInitialRoute, "route", "", "Which route to load when running the app.")
3330
runCmd.Flags().StringVarP(&runObservatoryPort, "observatory-port", "", "50300", "The observatory port used to connect hover to VM services (hot-reload/debug/..)")
34-
runCmd.Flags().BoolVar(&runOmitFlutterBundle, "omit-flutter", false, "Don't (re)compile the current Flutter project, useful when only working with Golang code (plugin)")
35-
runCmd.Flags().BoolVar(&runOmitEmbedder, "omit-embedder", false, "Don't (re)compile 'go-flutter' source code, useful when only working with Dart code")
3631
rootCmd.AddCommand(runCmd)
3732
}
3833

@@ -43,49 +38,23 @@ var runCmd = &cobra.Command{
4338
projectName := pubspec.GetPubSpec().Name
4439
assertHoverInitialized()
4540

46-
// ensure we have something to build
47-
if runOmitEmbedder && runOmitFlutterBundle {
48-
log.Errorf("Flags omit-embedder and omit-flutter are not compatible.")
49-
os.Exit(1)
50-
}
51-
5241
// Can only run on host OS
5342
targetOS := runtime.GOOS
5443

55-
// forcefully enable --debug as it is not optional for 'hover run'
56-
buildDebug = true
44+
initBuildParameters(targetOS, build.DebugMode)
45+
subcommandBuild(targetOS, packaging.NoopTask, []string{
46+
"--observatory-port=" + runObservatoryPort,
47+
"--enable-service-port-fallback",
48+
"--disable-service-auth-codes",
49+
})
5750

58-
if runOmitFlutterBundle {
59-
log.Infof("Omiting flutter build bundle")
60-
} else {
61-
// TODO: cleaning can't be enabled because it would break when users --omit-embedder.
62-
// cleanBuildOutputsDir(targetOS)
63-
buildFlutterBundle(targetOS)
64-
}
65-
if runOmitEmbedder {
66-
log.Infof("Omiting build the embedder")
67-
} else {
68-
vmArguments := []string{"--observatory-port=" + runObservatoryPort, "--enable-service-port-fallback", "--disable-service-auth-codes"}
69-
if buildOrRunDocker {
70-
var buildFlags []string
71-
buildFlags = append(buildFlags, commonFlags()...)
72-
buildFlags = append(buildFlags, []string{
73-
"--skip-flutter-build-bundle",
74-
"--skip-engine-download",
75-
"--debug",
76-
}...)
77-
dockerHoverBuild(targetOS, packaging.NoopTask, buildFlags, vmArguments)
78-
} else {
79-
buildGoBinary(targetOS, vmArguments)
80-
}
81-
}
8251
log.Infof("Build finished, starting app...")
8352
runAndAttach(projectName, targetOS)
8453
},
8554
}
8655

8756
func runAndAttach(projectName string, targetOS string) {
88-
cmdApp := exec.Command(dotSlash + filepath.Join(build.BuildPath, "build", "outputs", targetOS, config.GetConfig().GetExecutableName(projectName)))
57+
cmdApp := exec.Command(build.OutputBinaryPath(config.GetConfig().GetExecutableName(projectName), targetOS, buildOrRunMode))
8958
cmdApp.Env = append(os.Environ(),
9059
"GOFLUTTER_ROUTE="+runInitialRoute)
9160
cmdFlutterAttach := exec.Command("flutter", "attach")
@@ -123,7 +92,7 @@ func runAndAttach(projectName string, targetOS string) {
12392
// Non-blockingly echo command stderr to terminal
12493
go io.Copy(os.Stderr, stderrApp)
12594

126-
log.Infof("Running %s in debug mode", projectName)
95+
log.Infof("Running %s in %s mode", projectName, buildOrRunMode.Name)
12796
err = cmdApp.Start()
12897
if err != nil {
12998
log.Errorf("Failed to start app '%s': %v", projectName, err)

internal/build/build.go

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package build
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67

@@ -13,8 +14,8 @@ const BuildPath = "go"
1314

1415
// buildDirectoryPath returns the path in `BuildPath`/build.
1516
// If needed, the directory is create at the returned path.
16-
func buildDirectoryPath(targetOS, path string) string {
17-
outputDirectoryPath, err := filepath.Abs(filepath.Join(BuildPath, "build", path, targetOS))
17+
func buildDirectoryPath(targetOS string, mode Mode, path string) string {
18+
outputDirectoryPath, err := filepath.Abs(filepath.Join(BuildPath, "build", path, fmt.Sprintf("%s-%s", targetOS, mode.Name)))
1819
if err != nil {
1920
log.Errorf("Failed to resolve absolute path for output directory: %v", err)
2021
os.Exit(1)
@@ -32,8 +33,8 @@ func buildDirectoryPath(targetOS, path string) string {
3233
// OutputDirectoryPath returns the path where the go-flutter binary and flutter
3334
// binaries blobs will be stored for a particular platform.
3435
// If needed, the directory is create at the returned path.
35-
func OutputDirectoryPath(targetOS string) string {
36-
return buildDirectoryPath(targetOS, "outputs")
36+
func OutputDirectoryPath(targetOS string, mode Mode) string {
37+
return buildDirectoryPath(targetOS, mode, "outputs")
3738
}
3839

3940
// IntermediatesDirectoryPath returns the path where the intermediates stored.
@@ -42,8 +43,8 @@ func OutputDirectoryPath(targetOS string) string {
4243
// Those intermediates include the dynamic library dependencies of go-flutter plugins.
4344
// hover copies these intermediates from flutter plugins folder when `hover plugins get`, and
4445
// copies to go-flutter's binary output folder before build.
45-
func IntermediatesDirectoryPath(targetOS string) string {
46-
return buildDirectoryPath(targetOS, "intermediates")
46+
func IntermediatesDirectoryPath(targetOS string, mode Mode) string {
47+
return buildDirectoryPath(targetOS, mode, "intermediates")
4748
}
4849

4950
// OutputBinary returns the string of the executable used to launch the
@@ -66,24 +67,46 @@ func OutputBinary(executableName, targetOS string) string {
6667

6768
// OutputBinaryPath returns the path to the go-flutter Application for a
6869
// specified platform.
69-
func OutputBinaryPath(executableName, targetOS string) string {
70-
outputBinaryPath := filepath.Join(OutputDirectoryPath(targetOS), OutputBinary(executableName, targetOS))
70+
func OutputBinaryPath(executableName, targetOS string, mode Mode) string {
71+
outputBinaryPath := filepath.Join(OutputDirectoryPath(targetOS, mode), OutputBinary(executableName, targetOS))
7172
return outputBinaryPath
7273
}
7374

74-
// EngineFilename returns the name of the engine file from flutter for the
75-
// specified platform.
76-
func EngineFilename(targetOS string) string {
75+
// ExecutableExtension returns the extension of binary files on a given platform
76+
func ExecutableExtension(targetOS string) string {
7777
switch targetOS {
7878
case "darwin":
79-
return "FlutterEmbedder.framework"
79+
// no special filename
80+
return ""
8081
case "linux":
81-
return "libflutter_engine.so"
82+
// no special filename
83+
return ""
8284
case "windows":
83-
return "flutter_engine.dll"
85+
return ".exe"
8486
default:
85-
log.Errorf("%s has no implemented engine file", targetOS)
87+
log.Errorf("Target platform %s is not supported.", targetOS)
8688
os.Exit(1)
8789
return ""
8890
}
8991
}
92+
93+
// EngineFiles returns the names of the engine files from flutter for the
94+
// specified platform and build mode.
95+
func EngineFiles(targetOS string, mode Mode) []string {
96+
switch targetOS {
97+
case "darwin":
98+
return []string{"libflutter_engine.dylib"}
99+
case "linux":
100+
return []string{"libflutter_engine.so"}
101+
case "windows":
102+
if mode.IsAot {
103+
return []string{"flutter_engine.dll", "flutter_engine.exp", "flutter_engine.lib", "flutter_engine.pdb"}
104+
} else {
105+
return []string{"flutter_engine.dll"}
106+
}
107+
default:
108+
log.Errorf("%s has no implemented engine file", targetOS)
109+
os.Exit(1)
110+
return []string{}
111+
}
112+
}

internal/build/mode.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package build
2+
3+
type Mode struct {
4+
Name string
5+
IsAot bool
6+
}
7+
8+
var DebugMode = Mode{
9+
Name: "debug_unopt",
10+
IsAot: false,
11+
}
12+
13+
var ReleaseMode = Mode{
14+
Name: "release",
15+
IsAot: true,
16+
}
17+
18+
var ProfileMode = Mode{
19+
Name: "profile",
20+
IsAot: true,
21+
}

0 commit comments

Comments
 (0)