gccgo build can not find custom pakage - gccgo

when use the gccgo build one single go file is ok, but whn I build a large multi custom pacage go mod project, the project have some sub package(such as app/ app/core/server etc) not build success.
how to fix this? anyhelp? I build with -x and see some debug info.
go build -x -gccgoflags -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go
the error is:
WORK=/tmp/go-build1609005358
mkdir -p $WORK/b001/
cd $WORK
/opt/gccgo/bin/gccgo -fgo-importcfg=/dev/null -c -x c - -o /dev/null || true
mkdir -p $WORK/b001/_importcfgroot_/github.com/gin-gonic
ln -s /home/liangqi1/.cache/go-build/8a/8ae3d6bb3097698b7be6547599d7a61c9c5c54cca0be55c007ca1c8386d1188c-d $WORK/b001/_importcfgroot_/github.com/gin-gonic/libgin.a
mkdir -p $WORK/b001/_importcfgroot_/github.com/logrusorgru
ln -s /home/liangqi1/.cache/go-build/6f/6fb602f3c990310188a3e921909d152810f07d50f1ab4c621ab71b87afd4942d-d $WORK/b001/_importcfgroot_/github.com/logrusorgru/libaurora.a
mkdir -p $WORK/b001/_importcfgroot_/go.uber.org
ln -s /home/liangqi1/.cache/go-build/c0/c0ac9f2f0ebb74e0997bfa1d72d21a79ce906ed736197f83402365203094c3a9-d $WORK/b001/_importcfgroot_/go.uber.org/libautomaxprocs.a
/opt/gccgo/bin/gccgo -ffile-prefix-map=a=b -c -x c - -o /dev/null || true
cd /home/liangqi1/gccgo_demo
/opt/gccgo/bin/gccgo -c -g -m64 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -fgo-relative-import-path=_/home/liangqi1/gccgo_demo -o $WORK/b001/_go_.o -I $WORK/b001/_importcfgroot_ -Wl,-R,/opt/gccgo/lib64/ ./main.go
# command-line-arguments
./main.go:7:23: 错误:import file ‘gccgo_demo/app’ not found
7 | "gccgo_demo/app"
| ^
./main.go:8:35: 错误:import file ‘gccgo_demo/app/core/server’ not found
8 | "gccgo_demo/app/core/server"
| ^
./main.go:24:19: 错误:reference to undefined name ‘app’
24 | if err := app.Init(prjHome); err != nil {
| ^
./main.go:29:12: 错误:reference to undefined name ‘app’
29 | if app.IsProd() {
| ^
./main.go:35:9: 错误:reference to undefined name ‘server’
35 | server.Run()
| ^
the gccgo just clone from gcc git:
gccgo (GCC) 12.0.1 20220217
go version is 1.16.4
my project struct like flow:
gccgo_demo
├── app
│   ├── app.go
│   ...
├── go.mod
├── go.sum
├── main.go
... other code
the go mod name is gccgo_demo so , the app/app.go has some import like gccgo_demo/app path.

I don't see your code but I guess the problem that you do not pass the include path.
The cgo tool will always invoke the C compiler with the source file's directory in the include path; i.e. -I${SRCDIR} is always implied. This means that if a header file foo/bar.h exists both in the source directory and also in the system include directory (or some other place specified by a -I flag), then "#include <foo/bar.h>" will always find the local version in preference to any other version.
So just do smth like
go build -x -gccgoflags -I/path/to/your/file -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go
More details https://pkg.go.dev/cmd/cgo

Related

How to delete '$'\n' characters appended to ldd list

I am trying to copy binary dependencies to other folder, so I use ldd to see what should be copied.
However the script fails when copying. It "appears" '$'\n' characters when binary dependencies are copied.
Something is wrong, but I dont know what. Tried running command per command and cant see the fault.
What is the trouble here ?
Thanks
Script code
#!/usr/bin/env bash
set -e
chr=/home/myjail
cmds=( bash echo ls rm )
mkdir -p "$chr"/{bin,lib,lib64}
# copy commands
for app in "${cmds[#]}"; do
echo 'Added command:'"$app"
cp -v /bin/"$app" "$chr/bin"
done
# copy deps
for app in "${cmds[#]}";do
echo 'deps for:'"$app"
deps="$(ldd /bin/"$app" | egrep -o '/lib.*\.[0-9]')"
for curdep in "${deps[#]}";do
echo "$curdep"
cp -v --parents "$curdep" "$chr"
done
done
Script output
furby#debian-haptic20:~# ./depcopy.sh
Added command:bash
'/bin/bash' -> '/var/lib/haproxy/bin/bash'
Added command:echo
'/bin/echo' -> '/var/lib/haproxy/bin/echo'
Added command:ls
'/bin/ls' -> '/var/lib/haproxy/bin/ls'
Added command:mysql
'/bin/mysql' -> '/var/lib/haproxy/bin/mysql'
deps for:bash
/lib/x86_64-linux-gnu/libtinfo.so.6
/lib/x86_64-linux-gnu/libdl.so.2
/lib/x86_64-linux-gnu/libc.so.6
/lib64/ld-linux-x86-64.so.2
cp: failed to get attributes of '/lib/x86_64-linux-gnu/libtinfo.so.6'$'\n': No such file or directory
I'm using this script for this purpose:
copydep.sh
#!/bin/bash
if [ ${#} != 2 ]
then
echo "usage $0 PATH_TO_BINARY target_folder"
exit 1
fi
path_to_binary="$1"
target_folder="$2"
# if we cannot find the the binary we have to abort
if [ ! -f "${path_to_binary}" ]
then
echo "The file '${path_to_binary}' was not found. Aborting!"
exit 1
fi
# copy the binary itself
echo "---> copy binary itself"
cp --parents -v "${path_to_binary}" "${target_folder}"
# copy the library dependencies
echo "---> copy libraries"
ldd "${path_to_binary}" | awk -F'[> ]' '{print $(NF-1)}' | while read -r lib
do
[ -f "$lib" ] && cp -v --parents "$lib" "${target_folder}"
done
Run it like this:
$ mkdir /tmp/test
$ bash copydep.sh /bin/ls /tmp/test
---> copy binary itself
/bin -> /tmp/test/bin
'/bin/ls' -> '/tmp/test/bin/ls'
---> copy libraries
/lib -> /tmp/test/lib
/lib/x86_64-linux-gnu -> /tmp/test/lib/x86_64-linux-gnu
'/lib/x86_64-linux-gnu/libselinux.so.1' -> '/tmp/test/lib/x86_64-linux-gnu/libselinux.so.1'
'/lib/x86_64-linux-gnu/libc.so.6' -> '/tmp/test/lib/x86_64-linux-gnu/libc.so.6'
'/lib/x86_64-linux-gnu/libpcre.so.3' -> '/tmp/test/lib/x86_64-linux-gnu/libpcre.so.3'
'/lib/x86_64-linux-gnu/libdl.so.2' -> '/tmp/test/lib/x86_64-linux-gnu/libdl.so.2'
/lib64 -> /tmp/test/lib64
'/lib64/ld-linux-x86-64.so.2' -> '/tmp/test/lib64/ld-linux-x86-64.so.2'
'/lib/x86_64-linux-gnu/libpthread.so.0' -> '/tmp/test/lib/x86_64-linux-gnu/libpthread.so.0'
# Result:
$ tree /tmp/test/
/tmp/test/
├── bin
│   └── ls
├── lib
│   └── x86_64-linux-gnu
│   ├── libc.so.6
│   ├── libdl.so.2
│   ├── libpcre.so.3
│   ├── libpthread.so.0
│   └── libselinux.so.1
└── lib64
└── ld-linux-x86-64.so.2
4 directories, 7 files
Your deps variable should be an array, created with mapfile and some redirection of process substitution:
mapfile -t deps < <(ldd /bin/"$app" | grep -o '/lib.*\.[0-9]')
As you have it, it's treated as a single string, which as you've seen, doesn't work.

`go run: cannot run non-main package` as Unprivileged User

I'm encountering an error with a recently-compiled golang compiler, but only as an unprivileged user. I'm hoping someone could provide advice on further troubleshooting ideas.
As root:
$ cd $HOME && pwd
/root
$ mkdir -pv $HOME/go/src/hello
/bin/mkdir: created directory 'go'
/bin/mkdir: created directory 'go/src'
/bin/mkdir: created directory 'go/src/hello'
$ cd go/src/hello
$ cat > hello.go << EOF
> package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello, world\n")
> }
> EOF
$ go build
$ ls
hello hello.go
$ ./hello
hello, world
$ go run hello.go
hello, world
As an unprivileged user:
$ cd $HOME && pwd
/usr/src/libcap
$ mkdir -pv $HOME/go/src/hello
/bin/mkdir: created directory 'go'
/bin/mkdir: created directory 'go/src'
/bin/mkdir: created directory 'go/src/hello'
$ cd go/src/hello
$ cat > hello.go << EOF
> package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello, world\n")
> }
> EOF
$ go build
can't load package: package libcap/go/src/hello: cannot find package "libcap/go/src/hello" in any of:
/usr/src/libcap/go/src/hello (from $GOROOT)
/usr/src/libcap/go/src/libcap/go/src/hello (from $GOPATH)
$ ls
hello.go
$ go run hello.go
go run: cannot run non-main package
System information:
$ go version
go version go1.12.2 gccgo (GCC) 9.2.0 linux/amd64
I'm running in an LFS chroot environment and first discovered the issue while compiling the libcap package, which contains a go module, in chapter 6.28 of the 20200119-systemd book. The host is a Debian live cd:
$ uname -a
Linux debian 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u1 (2019-09-20) x86_64 GNU/Linux
The go tests appear to be fairly nominal, with very few unexpected failures (command executed from gcc build directory):
$ ../contrib/test_summary | grep -A7 Summ > log
$ less log
...
=== go Summary ===
# of expected passes 7349
# of unexpected failures 1
# of expected failures 1
# of untested testcases 6
# of unsupported tests 1
/usr/src/gcc/gcc-9.2.0/build/gcc/gccgo version 9.2.0 (GCC)
--
...
=== gotools Summary ===
# of expected passes 216
# of unexpected failures 4
# of untested testcases 159
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
...
=== libgo Summary ===
# of expected passes 178
# of unexpected failures 6
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
=== libgomp Summary ===
# of expected passes 6458
# of expected failures 2
# of unsupported tests 349
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
...

Build does not include source with file suffix

Compiling under ubuntu
Linux ip 3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015
x86_64 x86_64 x86_64 GNU/Linux
with golang version
go version go1.8.3 linux/amd64
The files I have:
daemon_darwin.go daemon_freebsd.go daemon.go daemon_linux.go
daemon_windows.go LICENSE os.go README.md
The command I'm executing
GOOS=darwin GOARCH=amd64 go build -x
The output I'm getting
WORK=/tmp/go-build026556308 mkdir -p
$WORK/github.com/VividCortex/godaemon/_obj/ mkdir -p
$WORK/github.com/VividCortex/ cd
/root/go/src/github.com/VividCortex/godaemon
/home/jenkinsBuilder/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go_1.8/pkg/tool/linux_amd64/compile
-o $WORK/github.com/VividCortex/godaemon.a -trimpath $WORK -p github.com/VividCortex/godaemon -complete -buildid
398bd35cdb7ed0aa42a3e6ea64677925441c29cb -D
_/root/go/src/github.com/VividCortex/godaemon -I $WORK -pack ./daemon.go ./os.go
# github.com/VividCortex/godaemon ./daemon.go:167: undefined: GetExecutablePath
we can see that daemon_darwin.go is not being included in the compile command, although I've added GOOS=darwin
If I try using GOOS=windows, it builds correctly, and daemon_windows.go is being added

Golang: go build failing when called as part of makefile

I have the following make file:
default: builddocker
setup:
go get github.com/golang/protobuf/proto
go get github.com/golang/protobuf/protoc-gen-go
go get golang.org/x/net/context
go get google.golang.org/grpc
buildgo:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
builddocker:
docker build -t johnwesonga/grpc/customer-server -f ./Dockerfile.build .
docker run -t johnwesonga/grpc/customer-server /bin/true
docker cp `docker ps -q -n=1`:/main .
chmod 755 ./main
docker build --rm=true --tag=johnwesonga/grpc/customer-server -f Dockerfile.static .
My Dockerfile.build has:
FROM golang
ADD Makefile /
WORKDIR /
RUN make setup
RUN make buildgo
CMD ["/bin/bash"]
But every time I run "make builddocker" I get the following error:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
can't load package: package ./customer-server: open /customer-server: no such file or directory
make: *** [buildgo] Error 1
Makefile:10: recipe for target 'buildgo' failed
The command '/bin/sh -c make buildgo' returned a non-zero code: 2
make: *** [builddocker] Error 2
But if I run "Make buildgo" it works just fine, any idea why this is failing?
FYI my source tree as follows:
/go/src/github.com/johnwesonga/grpc/MakeFile
/go/src/github.com/johnwesonga/grpc/customer-server
/go/src/github.com/johnwesonga/grpc/customer-server/main.go

gcov cannot open graph file

I'm getting the following error when trying to run gcov as part of my Travis CI build:
$ gcov src/bgrep.c
bgrep.gcno:cannot open graph file
My .travis.yml:
language: c
compiler: gcc
sudo: false
before_script:
- uname -a
- printenv
- gcov --version
script:
- scons coverage=1
- ls -l src/
- test/run_test.py
- ls -l src/
- gcov src/bgrep.c
- ls -l src/
- ls -l
after_success:
- bash <(curl -s https://codecov.io/bash)
I can run the exact sequence of commands (scons, test/run_test.py, gcov src/bgrep.c) on my Fedora machine, and gcov produces grep.gcov as expected.
Travis is using gcov (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3. You can see an example build log here.
What am I (or Travis) doing wrong?
I had the same problem with gcov and travis. It seems that gcov indeed requires that the *.gcda and *.gcno files must be in the same directory as the *.c files. And additionally the -o option must be given which points then to the same directory...
So, I have the source files in folder src/ and the binaries in build/coverage/. First, I tried with 'gcov -o build/coverage/ src/*.c' which did not work. Second attempt was with copying the *.gc* files to src/ and executing 'gcov src/*.c' which also did not work. It finally worked with 'gcov -o src/ src/*.c'.

Resources