How to build go with Go

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed.

This article is based on the part of the go build that I described in mid-April 2013 to the Sydney go user group share.

In mail groups and IRC channels, there are often questions about the go compiler, runtime, and internal implementation documentation. The current document on Go Internal implementation authority is the source code, I encourage everyone to see the source code. As I've said before, the go build process has been fixed since Go1.0 was released, so this article is likely to last a while.

This article starts with the source code and ends with the fully tested go installation process, which tells you the nine steps of the go build process. For simplicity, all the paths mentioned here are relative to the root of the checked out source code, $GOROOT/src.

As for background knowledge requirements, you should read the golang.org above from the source code to install go.

The first step, All.bash

% CD $GOROOT/src%./all.bash

The first step is a bit out of the way, because All.bash will invoke another two shell scripts: Make.bash and Run.bash. If you use Windows or PLAN9, the steps are the same, except that the script name ends with. bat and. RC respectively. In the following narrative, replace with the extension of the appropriate operating system.

The second step. Make.bash

. ./make.bash--no-banner

Make.bash originates from All.bash, so calling exit terminates the build process normally. Make.bash has three main functions, the first one is to verify the integrity of the environment that is compiling go. Integrity checks have been built up over the last few years to avoid building with known broken tools or in environments where build fails.

The third step. Cmd/dist

Gcc-o2-wall-werror-ggdb-o cmd/dist/dist-icmd/dist cmd/dist/*.c

Once the integrity check is complete, Make.bash compiles cmd/dist. Cmd/dist replaces the makefile-based build system prior to GO1 and manages a small amount of code generation in Pkg/runtime. Cmd/dist is a C program that enables it to use the system's C compiler and header files to handle most of the host platform detection issues. Cmd/dist always detects the host's operating system and architecture, $GOHOSTOS and $GOHOSTARCH. If you are cross-compiling, the values of these variables may not be the same as the settings. In fact, the go build process is building a cross compiler, but most of the time the host is consistent with the target machine platform. After that, Make.bash calls Cmd/dist with the bootstrap parameters, which first compiles the support libraries lib9, Libbio, and Libmach used by the compiler suite, and compiles the compiler itself. These tools are also written in C and compiled with the C compiler of the system.

echo "# Building compilers and Go bootstrap tool for Host, $GOHOSTOS/$GOHOSTARCH." Buildall= "-A" if ["$" = "--no-clean"]; Then buildall= "" Fi./cmd/dist/dist Bootstrap $buildall-V # builds Go_bootstrap

Cmd/dist compiled a version of the Go tool go_bootstrap with the compiler suite. Go_bootstrap is an incomplete go tool, for example, to avoid dependency on CGO and remove the pkg/net. Because the Cmd/dist tool contains a list of packages or libraries that will be compiled, and their dependent folders, you should be careful to avoid introducing new Cmd/go build dependencies.

Fourth step. Go_bootstrap

Now that the Go_bootstrap has been built, the final step of Make.bash is to compile all the Go standard libraries with go_bootstrap to include a full version of the Go tool.

echo "# Building packages and commands for $GOOS/$GOARCH." $GOTOOLDIR "/go_bootstrap install-gcflags" $GO _gcflags "\    -ldflags" $GO _ldflags "-V STD

Fifth step. Run.bash

Now the Make.bash is done. Execution control goes back to All.bash. Next, Run.bash is called. The role of Run.bash is to compile and test standard libraries, runtimes, and language test suites.

Bash Run.bash--no-rebuild

Using –no-rebuild is because both Make.bash and Run.bash call Go install-a std. To avoid repeated calls, –no-rebuild skips the second go install.

# allow All.bash to avoid double-build of Everythingrebuild=trueif ["$" = "--no-rebuild"]; Then Shiftelse Echo ' # Building packages and commands. ' Time go install-a-v STD Echofi

Sixth step. Go test-a STD

Echo ' # Testing packages ' time go test std-short-timeout=$ (expr \* $timeout _scale) s Echo

Run.bash Next runs unit tests in all packages in the standard library, which use the testing package. Because $gopath and $GOROOT code belong to the same namespace, we can't use go test ... (This tests all the packages in the $gopath). So using an alias Std to declare a package in a standard library, because some tests take a long time or consume a lot of memory, you can use the-short flag to filter the tests.

Seventh step. Runtime and CGO tests

Run.bash next runs a series of tests that support the CGO platform, a few benchmarks, and compiles a variety of miscellaneous programs with go. Slowly, this miscellaneous program is getting more and more. Because we found that these miscellaneous programs are indispensable in the construction process.

Eighth step. Go Run test

(XCD: /testunset gomaxprocstime Go run run.go) | | Exit $?

This is the second-to-last phase of Run.bash. Call the test case at the compiler and runtime under the $goroot Test folder. These are test cases for the compiler and the runtime's own underlying details. While testing the language specification, test cases under the Test/bugs and Test/fixedbugs subfolders run independently to capture previously discovered and fixed issues. All of these tests are driven by $GOROOT/test/run.go, It executes each of the. Go files under the test folder. Some. Go files contain instructions in the first line that indicate run.go, such as exiting a program or throwing a particular output sequence.

Nineth step. Go Tool API

Echo ' # Checking API compatibility. ' Go tool api-c $GOROOT/api/go1.txt, $GOROOT/api/go1.1.txt \    -next $GOROOT/api/ Next.txt-except $GOROOT/api/except.txt

The final step of Run.bash is to invoke the API tool. The role of the API tool is to execute the GO1 API conventions when Go1 was released in 2012, GO1 APIs include export symbols, constants, functions, variables, types, methods, and so on. Go1 writes these in Api/go1.txt, and Go 1.1 is written in Api/go1.1.txt. After Go1.1, an additional file api/next.txt indicates the standard library and runtime new symbols. Once Go1.2 is released, this file becomes the Go1.2 convention and will have a new next.txtt. There is also a small file except.txt that contains the GO1 convention exception. Modifications to these documents cannot be taken lightly.

Other Tips and Tricks

You may have found that Make.bash is useful for building go without running test cases, and Run.bash is used to build and test the GO runtime. The difference is that the former can be used for cross-compiling, which is helpful if you are based on standard library development.

Update: Thanks to Russ Cox and Andrew Gerrand for feedback and advice.

Dave Cheney, thanks to @codefor for his enthusiastic translation. If other friends also have a good original or translation, you can try to recommend to Bole online.

Link: http://blog.jobbole.com/41054/

"Non-special instructions, reproduced in the text must be marked and retained in the original link, translation links and translators and other information, thank you for your cooperation!" 】

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.