Build constraints being ignored

I’m stumped again. The docs seem pretty clear on this.

https://golang.org/pkg/go/build/

factorio_server_linux.go

// +build linux


package main


// Stubs for windows-only functions

func sendCtrlCToPid(pid int) {
}

func setCtrlHandlingIsDisabledForThisProcess(disabled bool) {
}

factorio_server_windows.go

// +build windows

package main

import (
	"log"
	"syscall"
)

func sendCtrlCToPid(pid int) {
	d, e := syscall.LoadDLL("kernel32.dll")
	if e != nil {
		log.Fatalf("LoadDLL: %v\n", e)
	}
	p, e := d.FindProc("GenerateConsoleCtrlEvent")
	if e != nil {
		log.Fatalf("FindProc: %v\n", e)
	}
	r, _, e := p.Call(uintptr(syscall.CTRL_C_EVENT), uintptr(pid))
	if r == 0 {
		log.Fatalf("GenerateConsoleCtrlEvent: %v\n", e)
	}
}

func setCtrlHandlingIsDisabledForThisProcess(disabled bool) {
	disabledInt := 0
	if(disabled){
		disabledInt = 1
	}
	
	d, e := syscall.LoadDLL("kernel32.dll")
	if e != nil {
		log.Fatalf("LoadDLL: %v\n", e)
	}
	p, e := d.FindProc("SetConsoleCtrlHandler")
	if e != nil {
		log.Fatalf("FindProc: %v\n", e)
	}
	r, _, e := p.Call(uintptr(0), uintptr(disabledInt))
	if r == 0 {
		log.Fatalf("SetConsoleCtrlHandler: %v\n", e)
	}
}

build commands:

GOOS=linux GOARCH=amd64 go build -o factorio-server-manager/factorio-server-manager src/*
GOOS=windows GOARCH=amd64 go build -o factorio-server-manager/factorio-server-manager src/*

This is on a brand-new Ubuntu 16.04 VM. I already wiped my golang-go install once, when I realized it was using 1.6, and reinstalled with 1.8, per https://github.com/golang/go/wiki/Ubuntu

The build errors I’m getting indicate that it’s definitely trying to build for linux and windows, respectively, as I get an “undefined” error for syscall.LoadDLL only when building for linux. However, regardless of the GOOS setting, both of the above files are being compiled, and I’m getting “redeclared” errors. I can even switch both of them to // +build ignore and they still end up attempting to build.

Also, just tried removing carriage returns.

Don’t give go build the names of source files, give it names of packages to build.

2 Likes

That makes perfect sense, actually. Good catch, thanks.

You also don’t need the // +build comments when your files are named _linux and _windows because those same constraints are made by the filenames.

Agreed, I’ve already removed them. I added them when I was being baffled by the fact that the files were compiling, in spite of their names.

1 Like

Whoa, good catch. Easy to miss when focusing on build constraints! :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.