编译时按体系结构排除go源文件 [英] Exclude go source files by architecture when compiling

查看:230
本文介绍了编译时按体系结构排除go源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Windows编写一个Go程序,其中包含几个程序包。其中一个包使用CGo来调用一些.h和.c文件中定义的一些函数。这些.c文件依赖于windows.h。



由于在Windows平台上开发令人难以置信的繁琐,我想对这个文件中的函数进行模拟并开发在Linux上。但是,当我尝试编译时,我得到:

 致命错误:windows.h:没有这样的文件或目录

由于go工具会尝试编译我的Windows相关文件。有没有办法解决这个问题?我知道把

  #ifdef .. 
导入x
#endif

并不是最佳做法,但在这种情况下,我需要一些东西来允许仅编译Linux文件。

解决方案

构建约束文档:


构建约束是以+ build构建的指令开始的行注释,包裹中应包含文件的条件。约束可能出现在任何类型的源文件中(而不仅仅是Go),但它们必须出现在文件顶部附近,仅在空白行和其他行注释之前。



为了将构建约束与包文档区分开来,一系列构建约束必须后跟一个空行。



构建约束被评估为空间分隔选项的OR ;每个选项评估为逗号分隔术语的AND;并且每个术语都是一个字母数字词,或者,在!之前,是它的否定。也就是构建约束:



  // + build linux,386 darwin,!cgo 




对应于布尔公式:




 (linux AND 386)OR(darwin AND(NOT cgo))




文件可能有多个构建约束。总体约束是个体约束的与。也就是构建约束:



  // + build linux darwin 
/ / + build 386




对应于布尔公式:




 (linux OR darwin)AND 386 

lockquote

在特定版本中,满足以下词语:




  • 目标操作系统,如运行时拼写.GOOS

  • 目标架构,如运行时拼写.GOARCH

  • 如果ctxt.CgoEnabled为true,则使用编译器gc或gccgo

  • cgo
  • go1.1,来自Go 1.1以上版本

  • ctxt.BuildTags中列出的任何其他字词




如果文件的名称在删除扩展名和可能的_test后缀后匹配以下任何一种模式:



  * _ GOOS 
* _GOARCH
* _GOOS_GOARCH




(例如:source_windows_amd64.go)或文字:



  GOOS 
GOARCH




(例如:windows.go)GOOS和GOARCH分别表示任何已知的操作系统和体系结构值,则该文件被认为具有隐含的构建约束条件,要求这些条款。



为了防止文件被构建:




  // + build忽略




(任何其他不满意的单词都可以使用,但忽略是常规的。)



仅在使用cgo时创建文件,并且仅在Linux和OS X上创建文件:



  // + build linux,cgo darwin,cgo 




<这种文件通常与之配对另一个文件实现了其他系统的默认功能,在这种情况下,该文件会带来约束:



  // + build!linux,!darwin!cgo 




命名a文件dns_windows.go将导致它仅在为Windows构建软件包时才被包含;同样,只有在为32位x86构建软件包时才会包含math_386.s。



I am writing a Go program for Windows which contains several packages. One of these packages is using CGo to call a few functions defined in some .h and .c files. These .c files are dependent on windows.h .

Since developing on the Windows platform is incredibly tedious I would like to make a mockup of the functions in this file and develop on Linux instead. But when I try to compile I get:

fatal error: windows.h: No such file or directory

Since the go tool tries to compile my Windows dependent files. Is there some way around this? I know that putting something like

#ifdef ..
import x
#endif

is not best practice but in this case I need something to allow compiling only the "Linux" files.

解决方案

Quoting from the build constraints documentation:

A build constraint is a line comment beginning with the directive +build that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments.

To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

A build constraint is evaluated as the OR of space-separated options; each option evaluates as the AND of its comma-separated terms; and each term is an alphanumeric word or, preceded by !, its negation. That is, the build constraint:

// +build linux,386 darwin,!cgo

corresponds to the boolean formula:

(linux AND 386) OR (darwin AND (NOT cgo))

A file may have multiple build constraints. The overall constraint is the AND of the individual constraints. That is, the build constraints:

// +build linux darwin
// +build 386

corresponds to the boolean formula:

(linux OR darwin) AND 386

During a particular build, the following words are satisfied:

  • the target operating system, as spelled by runtime.GOOS
  • the target architecture, as spelled by runtime.GOARCH
  • the compiler being used, either "gc" or "gccgo"
  • "cgo", if ctxt.CgoEnabled is true
  • "go1.1", from Go version 1.1 onward
  • any additional words listed in ctxt.BuildTags

If a file's name, after stripping the extension and a possible _test suffix, matches any of the following patterns:

*_GOOS
*_GOARCH
*_GOOS_GOARCH

(example: source_windows_amd64.go) or the literals:

GOOS
GOARCH

(example: windows.go) where GOOS and GOARCH represent any known operating system and architecture values respectively, then the file is considered to have an implicit build constraint requiring those terms.

To keep a file from being considered for the build:

// +build ignore

(any other unsatisfied word will work as well, but "ignore" is conventional.)

To build a file only when using cgo, and only on Linux and OS X:

// +build linux,cgo darwin,cgo

Such a file is usually paired with another file implementing the default functionality for other systems, which in this case would carry the constraint:

// +build !linux,!darwin !cgo

Naming a file dns_windows.go will cause it to be included only when building the package for Windows; similarly, math_386.s will be included only when building the package for 32-bit x86.

这篇关于编译时按体系结构排除go源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆