Skip to content

Commit 74e81e8

Browse files
committed
Fix windows DLL register
1 parent 42fe23b commit 74e81e8

File tree

15 files changed

+92
-141
lines changed

15 files changed

+92
-141
lines changed

cmd/build-naive/cmd.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ var allTargets = []Target{
3434
{OS: "win", CPU: "x64", GOOS: "windows", ARCH: "amd64"},
3535
{OS: "win", CPU: "arm64", GOOS: "windows", ARCH: "arm64"},
3636
{OS: "win", CPU: "x86", GOOS: "windows", ARCH: "386"},
37-
// iOS
3837
{OS: "ios", CPU: "arm64", GOOS: "ios", ARCH: "arm64", Platform: "iphoneos", Environment: "device"},
3938
{OS: "ios", CPU: "arm64", GOOS: "ios", ARCH: "arm64", Platform: "iphoneos", Environment: "simulator"},
4039
{OS: "ios", CPU: "x64", GOOS: "ios", ARCH: "amd64", Platform: "iphoneos", Environment: "simulator"},
41-
// tvOS
4240
{OS: "ios", CPU: "arm64", GOOS: "ios", ARCH: "arm64", Platform: "tvos", Environment: "device"},
4341
{OS: "ios", CPU: "arm64", GOOS: "ios", ARCH: "arm64", Platform: "tvos", Environment: "simulator"},
4442
{OS: "ios", CPU: "x64", GOOS: "ios", ARCH: "amd64", Platform: "tvos", Environment: "simulator"},
45-
// Android
4643
{OS: "android", CPU: "arm64", GOOS: "android", ARCH: "arm64"},
4744
{OS: "android", CPU: "x64", GOOS: "android", ARCH: "amd64"},
4845
{OS: "android", CPU: "arm", GOOS: "android", ARCH: "arm"},
@@ -71,7 +68,6 @@ func init() {
7168
}
7269

7370
func preRun(cmd *cobra.Command, args []string) {
74-
// Find project root (directory containing go.mod)
7571
workingDirectory, err := os.Getwd()
7672
if err != nil {
7773
log.Fatalf("failed to get working directory: %v", err)
@@ -92,13 +88,11 @@ func preRun(cmd *cobra.Command, args []string) {
9288
}
9389

9490
func parseTargets() []Target {
95-
// Validate libc option
9691
if libcStr != "" && libcStr != "glibc" && libcStr != "musl" {
9792
log.Fatalf("invalid libc: %s (expected glibc or musl)", libcStr)
9893
}
9994

10095
if targetStr == "" {
101-
// Default to host platform
10296
hostOS := runtime.GOOS
10397
hostArch := runtime.GOARCH
10498
for _, t := range allTargets {
@@ -135,7 +129,6 @@ func parseTargets() []Target {
135129
continue
136130
}
137131

138-
// Standard targets
139132
if len(parts) != 2 {
140133
log.Fatalf("invalid target format: %s (expected os/arch)", part)
141134
}
@@ -171,7 +164,6 @@ func parseAppleTarget(goos, goarch string, parts []string) Target {
171164
platform = "tvos"
172165
}
173166

174-
// Determine environment
175167
environment := "device"
176168
if len(parts) == 3 && parts[2] == "simulator" {
177169
environment = "simulator"
@@ -180,7 +172,6 @@ func parseAppleTarget(goos, goarch string, parts []string) Target {
180172
environment = "simulator"
181173
}
182174

183-
// Find matching target
184175
for _, t := range allTargets {
185176
if t.GOOS == "ios" && t.ARCH == goarch && t.Platform == platform && t.Environment == environment {
186177
return t
@@ -191,13 +182,11 @@ func parseAppleTarget(goos, goarch string, parts []string) Target {
191182
return Target{}
192183
}
193184

194-
// applyLibc applies the --libc flag to a target
195185
func applyLibc(target Target) Target {
196186
if libcStr == "" || libcStr == "glibc" {
197187
return target
198188
}
199189

200-
// musl is only supported on Linux
201190
if target.GOOS != "linux" {
202191
log.Fatalf("--libc=musl is only supported for Linux targets, not %s", target.GOOS)
203192
}
@@ -208,7 +197,6 @@ func applyLibc(target Target) Target {
208197
return target
209198
}
210199

211-
// hostToCPU converts Go GOARCH to GN cpu
212200
func hostToCPU(goarch string) string {
213201
switch goarch {
214202
case "amd64":

cmd/build-naive/cmd_build.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ func init() {
2525
mainCommand.AddCommand(commandBuild)
2626
}
2727

28-
// formatTargetLog returns a human-readable log string for a target
2928
func formatTargetLog(t Target) string {
3029
if t.Platform != "" {
31-
// Apple platform with Platform/Environment
3230
platformName := "iOS"
3331
if t.Platform == "tvos" {
3432
platformName = "tvOS"
@@ -44,10 +42,8 @@ func formatTargetLog(t Target) string {
4442
return fmt.Sprintf("%s/%s", t.GOOS, t.ARCH)
4543
}
4644

47-
// getOutputDirectory returns the GN output directory for a target
4845
func getOutputDirectory(t Target) string {
4946
if t.Platform != "" {
50-
// Apple platform: out/cronet-{platform}-{cpu}[-simulator]
5147
directory := fmt.Sprintf("out/cronet-%s-%s", t.Platform, t.CPU)
5248
if t.Environment == "simulator" {
5349
directory += "-simulator"
@@ -68,7 +64,6 @@ func build(targets []Target) {
6864
log.Print("Build complete!")
6965
}
7066

71-
// getExtraFlags returns the EXTRA_FLAGS for a target
7267
func getExtraFlags(t Target) string {
7368
flags := []string{
7469
fmt.Sprintf(`target_os="%s"`, t.OS),
@@ -77,7 +72,6 @@ func getExtraFlags(t Target) string {
7772
return strings.Join(flags, " ")
7873
}
7974

80-
// OpenWrt SDK configuration for each architecture
8175
type openwrtConfig struct {
8276
target string // OpenWrt target (e.g., "x86", "armsr")
8377
subtarget string // OpenWrt subtarget (e.g., "64", "generic")
@@ -86,10 +80,8 @@ type openwrtConfig struct {
8680
gccVer string // GCC version in SDK
8781
}
8882

89-
// getOpenwrtConfig returns the OpenWrt SDK configuration for a target
9083
func getOpenwrtConfig(t Target) openwrtConfig {
9184
// Use OpenWrt 23.05.5 as it's stable and widely available
92-
// GCC version varies by release
9385
switch t.CPU {
9486
case "x64":
9587
return openwrtConfig{
@@ -129,7 +121,6 @@ func getOpenwrtConfig(t Target) openwrtConfig {
129121
}
130122
}
131123

132-
// getOpenwrtFlags returns the OPENWRT_FLAGS environment variable value
133124
func getOpenwrtFlags(t Target) string {
134125
config := getOpenwrtConfig(t)
135126
return fmt.Sprintf(
@@ -138,7 +129,6 @@ func getOpenwrtFlags(t Target) string {
138129
)
139130
}
140131

141-
// runGetClang runs naiveproxy's get-clang.sh with appropriate EXTRA_FLAGS
142132
func runGetClang(t Target) {
143133
// For cross-compilation on Linux, we need to also build host sysroot first
144134
// because GN needs host sysroot in addition to target sysroot.
@@ -147,7 +137,6 @@ func runGetClang(t Target) {
147137
hostCPU := hostToCPU(runtime.GOARCH)
148138
needsHostSysroot := hostOS == "linux" && (t.OS == "linux" || t.OS == "android" || t.OS == "openwrt")
149139
if needsHostSysroot {
150-
// Run get-clang.sh with host target to ensure host sysroot is downloaded
151140
hostFlags := fmt.Sprintf(`target_os="linux" target_cpu="%s"`, hostCPU)
152141
log.Printf("Running get-clang.sh for host sysroot with EXTRA_FLAGS=%s", hostFlags)
153142
command := exec.Command("bash", "./get-clang.sh")
@@ -160,7 +149,6 @@ func runGetClang(t Target) {
160149
log.Fatalf("get-clang.sh (host) failed: %v", err)
161150
}
162151

163-
// Create symlink for host sysroot so GN can find it at the default location
164152
hostSysrootSource := filepath.Join(srcRoot, "out/sysroot-build/bullseye/bullseye_amd64_staging")
165153
hostSysrootDestination := filepath.Join(srcRoot, "build/linux/debian_bullseye_amd64-sysroot")
166154
if _, err := os.Stat(hostSysrootDestination); os.IsNotExist(err) {
@@ -175,7 +163,6 @@ func runGetClang(t Target) {
175163
extraFlags := getExtraFlags(t)
176164
env := []string{"EXTRA_FLAGS=" + extraFlags}
177165

178-
// For openwrt (musl), also set OPENWRT_FLAGS
179166
if t.OS == "openwrt" {
180167
openwrtFlags := getOpenwrtFlags(t)
181168
env = append(env, "OPENWRT_FLAGS="+openwrtFlags)
@@ -196,12 +183,10 @@ func runGetClang(t Target) {
196183
}
197184

198185
func buildTarget(t Target) {
199-
// Run get-clang.sh to ensure toolchain is available
200186
runGetClang(t)
201187

202188
outputDirectory := getOutputDirectory(t)
203189

204-
// Prepare GN args
205190
args := []string{
206191
"is_official_build=true",
207192
"is_debug=false",
@@ -239,7 +224,6 @@ func buildTarget(t Target) {
239224
fmt.Sprintf("target_cpu=\"%s\"", t.CPU),
240225
}
241226

242-
// Platform-specific args
243227
switch t.OS {
244228
case "mac":
245229
args = append(args, "use_sysroot=false")
@@ -257,7 +241,6 @@ func buildTarget(t Target) {
257241
args = append(args, "use_cfi_icall=false", "is_cfi=false")
258242
}
259243
case "openwrt":
260-
// OpenWrt uses musl libc
261244
config := getOpenwrtConfig(t)
262245
sysrootDirectory := fmt.Sprintf("out/sysroot-build/openwrt/%s/%s", config.release, config.arch)
263246
args = append(args,
@@ -295,7 +278,6 @@ func buildTarget(t Target) {
295278
)
296279
}
297280

298-
// Detect and use ccache/sccache
299281
if runtime.GOOS == "windows" {
300282
sccachePath, _ := exec.LookPath("sccache")
301283
if sccachePath != "" {
@@ -310,13 +292,11 @@ func buildTarget(t Target) {
310292

311293
gnArgs := strings.Join(args, " ")
312294

313-
// Determine GN path
314295
gnPath := filepath.Join(srcRoot, "gn", "out", "gn")
315296
if runtime.GOOS == "windows" {
316297
gnPath += ".exe"
317298
}
318299

319-
// Run gn gen
320300
log.Printf("Running: gn gen %s", outputDirectory)
321301
gnCommand := exec.Command(gnPath, "gen", outputDirectory, "--args="+gnArgs)
322302
gnCommand.Dir = srcRoot
@@ -331,7 +311,6 @@ func buildTarget(t Target) {
331311
log.Fatalf("gn gen failed: %v", err)
332312
}
333313

334-
// Run ninja
335314
if t.GOOS == "windows" {
336315
// Windows: only build DLL (static linking not supported - Chromium uses MSVC, Go CGO only supports MinGW)
337316
log.Printf("Running: ninja -C %s cronet", outputDirectory)

cmd/build-naive/cmd_cgo_flags.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,24 @@ func init() {
2828
func printCGOFlags(t Target) {
2929
outputDirectory := fmt.Sprintf("out/cronet-%s-%s", t.OS, t.CPU)
3030

31-
// Extract linking flags from ninja file
3231
linkFlags, err := extractLinkFlags(outputDirectory)
3332
if err != nil {
3433
log.Fatalf("failed to extract link flags: %v", err)
3534
}
3635

3736
var ldFlags []string
3837

39-
// Add static library reference
4038
libraryDirectory := filepath.Join(projectRoot, "lib", getLibraryDirectoryName(t))
4139
if t.GOOS == "darwin" || t.GOOS == "ios" {
4240
ldFlags = append(ldFlags, filepath.Join(libraryDirectory, "libcronet.a"))
4341
} else {
4442
ldFlags = append(ldFlags, "-L"+libraryDirectory, "-l:libcronet.a")
4543
}
4644

47-
// Add extracted ldflags (e.g., -Wl,-wrap,* for Android)
4845
ldFlags = append(ldFlags, linkFlags.LDFlags...)
49-
50-
// Add extracted libs
5146
ldFlags = append(ldFlags, linkFlags.Libs...)
52-
53-
// Add extracted frameworks
5447
ldFlags = append(ldFlags, linkFlags.Frameworks...)
5548

56-
// Add Linux-specific flags
5749
if t.GOOS == "linux" {
5850
ldFlags = append(ldFlags, "-fuse-ld=lld")
5951
// Add -no-pie for 32-bit (required for position-dependent code)

cmd/build-naive/cmd_env.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func init() {
2424
mainCommand.AddCommand(commandEnv)
2525
}
2626

27-
// getClangTarget returns the clang target triple for a target
2827
func getClangTarget(t Target) string {
2928
if t.Libc == "musl" {
3029
switch t.CPU {
@@ -51,7 +50,6 @@ func getClangTarget(t Target) string {
5150
return ""
5251
}
5352

54-
// getSysrootPath returns the sysroot path for a target
5553
func getSysrootPath(t Target) string {
5654
if t.Libc == "musl" {
5755
config := getOpenwrtConfig(t)

0 commit comments

Comments
 (0)