[short] skip
[!cgo] skip

# Test building cgo packages in overlays.

cd m

go build -overlay overlay.json -o main_cgo_replace$GOEXE ./cgo_hello_replace
exec ./main_cgo_replace$GOEXE
stdout '^hello cgo\r?\n'

go build -overlay overlay.json -o main_cgo_quote$GOEXE ./cgo_hello_quote
exec ./main_cgo_quote$GOEXE
stdout '^hello cgo\r?\n'

go build -overlay overlay.json -o main_cgo_angle$GOEXE ./cgo_hello_angle
exec ./main_cgo_angle$GOEXE
stdout '^hello cgo\r?\n'

go list -compiled -overlay overlay.json -f '{{range .CompiledGoFiles}}{{. | printf "%s\n"}}{{end}}' ./cgo_hello_replace
cp stdout compiled_cgo_sources.txt
go run ../print_line_comments.go compiled_cgo_sources.txt
stdout $GOPATH[/\\]src[/\\]m[/\\]cgo_hello_replace[/\\]cgo_hello_replace.go
! stdout $GOPATH[/\\]src[/\\]m[/\\]overlay[/\\]hello.c

-- m/go.mod --
// TODO(matloob): how do overlays work with go.mod (especially if mod=readonly)
module m

go 1.16

-- m/overlay.json --
{
	"Replace": {
		"printpath/main.go": "overlay/printpath.go",
		"printpath/other.go": "overlay2/printpath2.go",
		"call_asm/asm_gc.s": "overlay/asm_gc.s",
		"call_asm/asm_gccgo.s": "overlay/asm_gccgo.s",
		"test_cache/main.go": "overlay/test_cache.go",
		"cgo_hello_replace/cgo_header.h": "overlay/cgo_head.h",
		"cgo_hello_replace/hello.c": "overlay/hello.c",
		"cgo_hello_quote/cgo_hello.go": "overlay/cgo_hello_quote.go",
		"cgo_hello_quote/cgo_header.h": "overlay/cgo_head.h",
		"cgo_hello_angle/cgo_hello.go": "overlay/cgo_hello_angle.go",
		"cgo_hello_angle/cgo_header.h": "overlay/cgo_head.h"
	}
}
-- m/cgo_hello_replace/cgo_hello_replace.go --
package main

// #include "cgo_header.h"
import "C"

func main() {
	C.say_hello()
}
-- m/cgo_hello_replace/cgo_header.h --
 // Test that this header is replaced with one that has the proper declaration.
void say_goodbye();

-- m/cgo_hello_replace/hello.c --
#include <stdio.h>

void say_goodbye() { puts("goodbye cgo\n"); fflush(stdout); }

-- m/overlay/cgo_hello_quote.go --
package main

// #include "cgo_header.h"
import "C"

func main() {
	C.say_hello()
}
-- m/overlay/cgo_hello_angle.go --
package main

// #include <cgo_header.h>
import "C"

func main() {
	C.say_hello()
}
-- m/overlay/cgo_head.h --
void say_hello();
-- m/overlay/hello.c --
#include <stdio.h>

void say_hello() { puts("hello cgo\n"); fflush(stdout); }
-- m/cgo_hello_quote/hello.c --
#include <stdio.h>

void say_hello() { puts("hello cgo\n"); fflush(stdout); }
-- m/cgo_hello_angle/hello.c --
#include <stdio.h>

void say_hello() { puts("hello cgo\n"); fflush(stdout); }

-- print_line_comments.go --
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

func main() {
	compiledGoFilesArg := os.Args[1]
	b, err := ioutil.ReadFile(compiledGoFilesArg)
	if err != nil {
		log.Fatal(err)
	}
	compiledGoFiles := strings.Split(strings.TrimSpace(string(b)), "\n")
	for _, f := range compiledGoFiles {
		b, err := ioutil.ReadFile(f)
		if err != nil {
			log.Fatal(err)
		}
		for _, line := range strings.Split(string(b), "\n") {
			if strings.HasPrefix(line, "#line") || strings.HasPrefix(line, "//line") {
				fmt.Println(line)
			}
		}
	}
}
