Purpose
I’m trying to modify the golang compiler(release 1.17.7) to collect information located in source code directory of packages which are imported by current project, and gather the information under the root directory(i.e. the main package directory) of the module.
What I do
What I do is that I add a flag -s
to allow the go compiler(gc) to dump information in each package source directory. I achieve it.
And then I try to collect these information. In the runBuild
function which is the entry of the gc dispatching compilation tasks, I try to add the following code to collect information dumped in each package’s source code directory to the main package’s directory after b.Do(ctx, a)
Line448 and b.Do(ctx, a)
Line459 which is the real function to do the compilation tasks. I think all imported packages will be compiled after b.Do(ctx, a)
finished. But the collect failed.
b.Do(ctx, a)
var main_path string
for _, p := range pkgs {
if p.Name == "main" {
main_path = p.Dir // get the main package's path
}
}
// collect information under each pkg.Dir and move files from pkg.Dir to
collect(pkgs, main_path)
What happened?
Take the following project as an example
pkg_export # the main package
|---main.go
|---mypkg # the mypkg package---imported by main.go
|---utils.go
In my experiment, using command go build -a -gcflags=all=-s .
, each package can properly dump information in each package’s source code directory, but the collect function can’t collect information from each package’s source directory to the main_path
. In the example project, it appears like:
pkg_export # the main package
|---main.go
|---info1.txt
|---mypkg # the mypkg package---imported by main.go
|---utils.go
|---info2.txt
the info2.txt
under mypkg
directory is not merged into the info1.txt
under pkg_export
directory after collect(pkgs, main_path)
function call.
What expected
the info2.txt
under mypkg
directory is not merged into the info1.txt
under pkg_export
directory after collect(pkgs, main_path)
function call.
My questions
My questions are:
- Is my code inserted in the right place? My purpose is to collect information when every dependency of the module is compiled and information is generated. Does all dependency compilation finish after
b.Do(ctx, a)
Line 547 andb.Do(ctx, a)
Line 536? - Do I use the correct field
p.Dir
of theload.Package
struct? - Is the compilation process parallel? If so, does the parallelism cause the failure of merging information?
Example source code
An example project is in Lslightly/pkg_export (github.com)
The rebuild of gc is to use ./clean.bash
and then ./make.bash
under src
directory of golang’s source code and the new go compiler will be generated.
I really appreciate any helps and ideas. Thanks!