Go compilation Process Analysis (i)--compile script

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

Go language is very hot recently, with the Times, I also looked at the grammar of Go.

It looks like go is good, there are a lot of new feature. Just downloaded the code to study a bit.

There are three sets of compiled files under the src directory of Go:

    1. Window platform, all bat files
    2. Plan9 platform, all RC files
    3. Unix class platform, all bash files

In the case of Unix-compiled files, go compiles the entry at Src/all.bash, which is a bash step, which simply calls the Make.bash after the footstep ends, calling Dist banner output compiled information.

Set-eif [!-f Make.bash]; Then        Echo ' All.bash must is run from $GOROOT/src ' 1>&2        exit 1fioldpath= "$PATH":/make.bash "$@"--no-bann Erbash run.bash--no-rebuildpath= "$OLDPATH" $GOTOOLDIR/dist Banner  # Print Build Info

Dist is an executable file generated in Make.bash, and all the compilation of Go is done under the control of this file. Personally think this is not a good design, resulting in the maintenance of the success of the compilation system is too high, if you want to modify the compilation options, often to modify the dist source code. The code for Dist is in the directory:/src/cmd/dist.

Dist This command-line program supports several parameters:

   "Banner         print installation banner\n"; Some information on printing and installation   "Bootstrap      rebuild everything\n"        ; recompile all go Codes   "Clean          Deletes all built files\n"   ; clear compiled go code   "env [-p]       print environment (-p:include $PATH) \ n"  ; Print compiled environment   "Install [dir] install  individual directory\n"  ; Install a directory. Will compile directory code, install build file   "version        print Go version\n"              ; about Go version information

To study the details of the compilation be sure to look at the code of this program, followed by detailed analysis.

Make.bash, also is a bash footsteps, open this footsteps, can see this step mainly do the following several things:

    1. Perform some initialization work based on different systems and parameters
    2. Compile build dist, call Dist to complete the entire go compilation. Dist Bootstrap
    3. Complete the installation process with the go_bootstrap generated by the compilation

Unfortunately, this script does not support window.  Window is lowered with Make.bat to complete the compilation. Go compiler system does not support Cygwin very well, this is the place which makes people feel very uncomfortable. In fact, the entire go compilation should be based on the makefile mechanism, and modify the Go compiler script, so that the entire source code does not rely on dist to complete the entire compilation process, is to let go very good support for a variety of different platforms good starting point.

There are some environment variables that are tightly coupled with make.bash and control some of the options for compilation:

    1. Goroot_final: This variable is used to indicate the path of Go final installation, if not set, the default value is the path of the current source code
    2. Gohostarch: Set the ARCH (architecture) of the computer that compiles the go voice, 386 or AMD64 or arm
    3. Goarch: Build the ARCH that the generated go runs on.
    4. GOOS: Build allowed operating system for Go
    5. Go_gcflags: Additional parameters specified when compiling 5g/6g/8g
    6. Go_ldflags: Additional parameters specified when compiling 5l/6l/8l
    7. Go_ccflags: Additional parameters specified when compiling 5c/6c/8c
    8. Cgo_enabled: Support CGO, set to 1, CGO related files will be compiled, set to 0, it will not compile
    9. Go_extlink_enabled: Whether to use link for the host environment. If you set 1, you will use the connector in the compilation environment, 0, without
    10. CC: Set C compiler name, GCC or clang, this setting is the compiler of the host environment
    11. Cc_for_target: Sets the C compiler name, which is the compiler that can generate the target environment code
    12. Cxx_for_target: Set CXX compiler name, g++ or clang++ This setting is the compiler capable of generating target environment code
    13. Go_distflags: Provides additional parameters for the Dist bootstrap.

Some analysis of Make.bash:

Determine if Run.bash exists, does not exist, then prompts, exits

if [!-f Run.bash]; Then        Echo ' Make.bash must is run from $GOROOT/src ' 1>&2        exit 1fi

Determine whether you are currently running in Cygwin or MinGW, or in a different window environment. Here, Cygwin. It's not appropriate to simply row into the window environment.

Case "$ (uname)" in*mingw* | *win32* | *cygwin*)       echo ' error:do not use Make.bash to build on Windows. '       Echo ' use Make.bat instead. '       echo       exit 1       ;; Esac
If you are currently a Darwin system, add a condition that sets the minimum MacOS version to the compilation option

If ["$ (uname)" = = "Darwin"]; Then        # golang.org/issue/5261        mflag= "$mflag-mmacosx-version-min=10.6" fi
If CC is not set and GCC does not exist on the host environment, Clang is present on the host environment, the compiler is set to Clang

# If GCC does not exist and $CC are not set, try Clang if available.if [-Z ' $CC "-a-z" $ (type-t gcc) "-a-n" $ (type-t C Lang) "]; Then        export Cc=clang Cxx=clang++fi

Compile and build the Dist program to determine if the compilation was successful

${CC:-GCC} $mflag-o2-wall-o cmd/dist/dist-icmd/dist "$DEFGOROOT" cmd/dist/*.c#-e doesn ' t propagate out of eval, so CH Eck Success by Hand.eval $ (./cmd/dist/dist Env-p | | echo fail=true) IF ["$FAIL" = true]; Then        exit 1fi
If the script is called when passed, such as the parameter "--dist--tool", it means that only compile dist, then after the disk is generated, install dist, and then exit

If ["$" = "--dist-tool"]; Then        # Stop after building dist tool.        Mkdir-p "$GOTOOLDIR"        If ["$" = ""]; Then                CP cmd/dist/dist "$"        fi        mv Cmd/dist/dist "$GOTOOLDIR"/dist        exit 0fi
Otherwise, recompile the code, compile Go_bootstrap, and execute the command Dist bootstrap

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

Complete the entire compilation process with Go_bootstrap

If ["$GOHOSTARCH"! = "$GOARCH"-O "$GOHOSTOS"! = "$GOOS"]; Then        echo "# Building packages and commands for host, $GOHOSTOS/$GOHOSTARCH."        # Cc_for_target is recorded as the default compiler for the Go tool. When building is the host, however,        # Use the host compiler, CC, from ' cmd/dist/dist env ' instead.        cc= $CC goos= $GOHOSTOS goarch= $GOHOSTARCH \                "$GOTOOLDIR"/go_bootstrap install-ccflags "$GO _ccflags"-gcflags "$GO _ Gcflags "-ldflags" $GO _ldflags "-V std        echofiecho" # Building packages and commands for $GOOS/$GOARCH. " cc= $CC _for_target "$GOTOOLDIR"/go_bootstrap install $GO _flags-ccflags "$GO _ccflags"-gcflags "$GO _gcflags"-ldflags "$ Go_ldflags "-V Stdecho


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.