[Proposal] Enable CGO as much as possible
- Dominant language
- Go
- Stars
- 6.2k
- Forks
- 404
- Avg merge
- 57m
- Merged PRs (30d)
- 2
Description
### Proposal
在编译参数 CGO_ENABLED=0 的情况下,lima 虚拟机内的 dae 始终 hang 在:
```
$ sudo ./dae run -c /etc/dae/config.dae
[Jun 21 02:00:36] INFO Include config files: [/etc/dae/config.dae]
[Jun 21 02:00:36] INFO Waiting for network...
```
编译参数 CGO_ENABLED=1 时一切正常。这可能是 go resolver 不支持 /etc/nsswitch.conf 导致的。
### Use Cases
一些特殊场景(例如 DNS 特异环境),提高兼容性。
### Potential Benefits
避免一些异常的 go resolver 行为。
### Scope
_No response_
### Reference
_No response_
### Implementation
最简单地,可开启 CGO_ENABLED=1,使得 DNS 工作正常:
```sh
make CGO_ENABLED=1
```
这也是打包时的建议方案,因此建议修改 Makefile 默认 CGO_ENABLED=1。
为了提高基础库版本兼容性,可使用 musl-gcc 来尽可能开启 CGO 的情况下保持静态编译:
```sh
sudo apt install musl-tools
export CGO_ENABLED=1
export CC=musl-gcc
export BUILD_ARGS='-ldflags "-s -w -linkmode external -extldflags=-static"'
make
```
```
$ sudo ./dae run -c /etc/dae/config.dae
[Jun 21 02:25:30] INFO Include config files: [/etc/dae/config.dae]
[Jun 21 02:25:30] INFO Waiting for network...
[Jun 21 02:25:31] INFO Network online.
[Jun 21 02:25:31] INFO Fetching subscriptions...
[Jun 21 02:25:32] INFO Loading eBPF programs and maps into the kernel...
[Jun 21 02:25:32] INFO The loading process takes about 120MB free memory, which will be released after loading. Insufficient memory will cause loading failure.
[Jun 21 02:25:33] INFO Loaded eBPF programs and maps
```
CI 里可以利用 zig 编译器来支持交叉编译:https://hrishikeshpathak.com/tips/build-static-binary-cross-compile-cgo-project-zig-compiler/
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the Makefile and its CGO_ENABLED default, then review the documented CGO_ENABLED=1 and musl-gcc build commands. Verify the resulting dae binary with the shown `dae run -c /etc/dae/config.dae` startup path, including network readiness and subscription fetching, and confirm the intended build configuration works for the relevant packaging or CI cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, zig
- Domain
- build-system, networking
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100