bugst / go-serial

A cross-platform serial library for go-lang.
BSD 3-Clause "New" or "Revised" License
619 stars 189 forks source link

I have a problem if using serial.v1/enumerator on macOS High Sierra (10.13.5) #50

Closed gibranv closed 5 years ago

gibranv commented 6 years ago

Hi I'm trying to get information about the connected serial devices using the serial.v1/enumerator library but I get the following error:

go build go.bug.st/serial.v1/enumerator: invalid flag in #cgo LDFLAGS: -fconstant-cfstrings

My sample code used which comes from the godoc documentation of the serial.v1 library (https://godoc.org/go.bug.st/serial.v1) is:

package main

import "go.bug.st/serial.v1"
import "go.bug.st/serial.v1/enumerator"
import "fmt"
import "log"

func main() {

    ports, err := serial.GetPortsList()
    if err != nil {
        log.Fatal(err)
    }   
    if len(ports) == 0 { 
        log.Fatal("No serial ports found!")
    }   
    for _, port := range ports {
        fmt.Printf("Found port: %v\n", port);
    }   

    ports, err = enumerator.GetDetailedPortsList()
    if err != nil {
        log.Fatal(err)
    }   
    if len(ports) == 0 { 
        log.Fatal("No serial ports found!")
    }   
    for _, port := range ports {
        fmt.Printf("Found port: %v\n", port);
    }   
}

go env:

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/gibranvargas/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/gibranvargas/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/vm/fs2hwdyd6gg88yt_k41zmjwm0000gp/T/go-build854035366=/tmp/go-build -gno-record-gcc-switches -fno-common"

I tried the code in a Windows and Linux environment and I dont get the error. Has anybody encounter this issue?

borud commented 5 years ago

Same exact problem observed on OSX El Capitan 10.11.6

cmaglie commented 5 years ago

It seems that golang has introduced restrictions on the cgo LDFLAGS, this is security related: https://github.com/golang/go/issues/23749.

BTW the good news is that it seems the flag -fconstant-cfstrings has been whitelisted and the patch will be availbale in go1.11: https://github.com/golang/go/commit/f7c2a71632854050d9fe6de115c6c346fcd9774c#diff-4edde0d5efc092a14e579fd0de312bdeR51

So, until go1.11 is published, some quick workarounds: