...

Source file src/github.com/google/pprof/internal/binutils/testdata/build_binaries.go

Documentation: github.com/google/pprof/internal/binutils/testdata

     1  // Copyright 2019 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // This is a script that generates the test executables for MacOS and Linux
    16  // in this directory. It should be needed very rarely to run this script.
    17  // It is mostly provided as a future reference on how the original binary
    18  // set was created.
    19  
    20  // When a new executable is generated, hardcoded addresses in the
    21  // functions TestObjFile, TestMachoFiles, TestPEFile in binutils_test.go must be updated.
    22  package main
    23  
    24  import (
    25  	"log"
    26  	"os"
    27  	"os/exec"
    28  	"path/filepath"
    29  	"runtime"
    30  )
    31  
    32  func main() {
    33  	wd, err := os.Getwd()
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  
    38  	switch runtime.GOOS {
    39  	case "linux":
    40  		if err := removeGlob("exe_linux_64*"); err != nil {
    41  			log.Fatal(err)
    42  		}
    43  
    44  		out, err := exec.Command("cc", "-g", "-ffile-prefix-map="+wd+"="+"/tmp", "-o", "exe_linux_64", "hello.c").CombinedOutput()
    45  		log.Println(string(out))
    46  		if err != nil {
    47  			log.Fatal(err)
    48  		}
    49  
    50  	case "darwin":
    51  		if err := removeGlob("exe_mac_64*", "lib_mac_64"); err != nil {
    52  			log.Fatal(err)
    53  		}
    54  
    55  		out, err := exec.Command("clang", "-g", "-ffile-prefix-map="+wd+"="+"/tmp", "-o", "exe_mac_64", "hello.c").CombinedOutput()
    56  		log.Println(string(out))
    57  		if err != nil {
    58  			log.Fatal(err)
    59  		}
    60  
    61  		out, err = exec.Command("clang", "-g", "-ffile-prefix-map="+wd+"="+"/tmp", "-o", "lib_mac_64", "-dynamiclib", "lib.c").CombinedOutput()
    62  		log.Println(string(out))
    63  		if err != nil {
    64  			log.Fatal(err)
    65  		}
    66  
    67  	case "windows":
    68  		// Many gcc environments may create binaries that trigger false-positives
    69  		// in antiviruses. MSYS2 with gcc 10.2.0 is a working environment for
    70  		// compiling. To setup the environment follow the guide at
    71  		// https://www.msys2.org/ and install gcc with `pacman -S gcc`.
    72  		out, err := exec.Command("gcc", "-g", "-ffile-prefix-map="+wd+"=", "-o", "exe_windows_64.exe", "hello.c").CombinedOutput()
    73  		log.Println(string(out))
    74  		if err != nil {
    75  			log.Fatal(err)
    76  		}
    77  		log.Println("Please verify that exe_windows_64.exe does not trigger any antivirus on `virustotal.com`.")
    78  	default:
    79  		log.Fatalf("Unsupported OS %q", runtime.GOOS)
    80  	}
    81  }
    82  
    83  func removeGlob(globs ...string) error {
    84  	for _, glob := range globs {
    85  		matches, err := filepath.Glob(glob)
    86  		if err != nil {
    87  			return err
    88  		}
    89  		for _, p := range matches {
    90  			os.Remove(p)
    91  		}
    92  	}
    93  	return nil
    94  }
    95  

View as plain text