Commit a4628167 authored by Alex Brainman's avatar Alex Brainman

build: multiple fixes to make "go install" work on windows

R=rsc
CC=golang-dev
https://golang.org/cl/5502054
parent eecb6a79
...@@ -545,4 +545,4 @@ mkdir -p $WORK/cmd/go/_obj/ ...@@ -545,4 +545,4 @@ mkdir -p $WORK/cmd/go/_obj/
gopack grc $WORK/cmd/go.a $WORK/cmd/go/_obj/_go_.6 gopack grc $WORK/cmd/go.a $WORK/cmd/go/_obj/_go_.6
8l -o $WORK/cmd/go/_obj/a.out -L $WORK $WORK/cmd/go.a 8l -o $WORK/cmd/go/_obj/a.out -L $WORK $WORK/cmd/go.a
mkdir -p $GOBIN/ mkdir -p $GOBIN/
cp $WORK/cmd/go/_obj/a.out $GOBIN/go cp $WORK/cmd/go/_obj/a.out $GOBIN/go.exe
...@@ -544,4 +544,4 @@ mkdir -p $WORK/cmd/go/_obj/ ...@@ -544,4 +544,4 @@ mkdir -p $WORK/cmd/go/_obj/
gopack grc $WORK/cmd/go.a $WORK/cmd/go/_obj/_go_.6 gopack grc $WORK/cmd/go.a $WORK/cmd/go/_obj/_go_.6
6l -o $WORK/cmd/go/_obj/a.out -L $WORK $WORK/cmd/go.a 6l -o $WORK/cmd/go/_obj/a.out -L $WORK $WORK/cmd/go.a
mkdir -p $GOBIN/ mkdir -p $GOBIN/
cp $WORK/cmd/go/_obj/a.out $GOBIN/go cp $WORK/cmd/go/_obj/a.out $GOBIN/go.exe
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"strings" "strings"
"sync" "sync"
) )
...@@ -518,6 +519,33 @@ func (b *builder) install(a *action) error { ...@@ -518,6 +519,33 @@ func (b *builder) install(a *action) error {
return b.copyFile(dst, src, perm) return b.copyFile(dst, src, perm)
} }
// removeByRenaming removes file name by moving it to a tmp
// directory and deleting the target if possible.
func removeByRenaming(name string) error {
f, err := ioutil.TempFile("", "")
if err != nil {
return err
}
tmpname := f.Name()
f.Close()
err = os.Remove(tmpname)
if err != nil {
return err
}
err = os.Rename(name, tmpname)
if err != nil {
// assume name file does not exists,
// otherwise later code will fail.
return nil
}
err = os.Remove(tmpname)
if err != nil {
// TODO(brainman): file is locked and can't be deleted.
// We need to come up with a better way of doing it.
}
return nil
}
// copyFile is like 'cp src dst'. // copyFile is like 'cp src dst'.
func (b *builder) copyFile(dst, src string, perm uint32) error { func (b *builder) copyFile(dst, src string, perm uint32) error {
if b.nflag || b.xflag { if b.nflag || b.xflag {
...@@ -535,7 +563,19 @@ func (b *builder) copyFile(dst, src string, perm uint32) error { ...@@ -535,7 +563,19 @@ func (b *builder) copyFile(dst, src string, perm uint32) error {
os.Remove(dst) os.Remove(dst)
df, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) df, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil { if err != nil {
return err if runtime.GOOS != "windows" {
return err
}
// Windows does not allow to replace binary file
// while it is executing. We will cheat.
err = removeByRenaming(dst)
if err != nil {
return err
}
df, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
} }
_, err = io.Copy(df, sf) _, err = io.Copy(df, sf)
df.Close() df.Close()
......
...@@ -285,7 +285,7 @@ func allPackages() []string { ...@@ -285,7 +285,7 @@ func allPackages() []string {
if err != nil { if err != nil {
return nil return nil
} }
name := path[len(src):] name := filepath.ToSlash(path[len(src):])
if !have[name] { if !have[name] {
pkgs = append(pkgs, name) pkgs = append(pkgs, name)
have[name] = true have[name] = true
......
...@@ -113,6 +113,9 @@ func scanPackage(ctxt *build.Context, t *build.Tree, arg, importPath, dir string ...@@ -113,6 +113,9 @@ func scanPackage(ctxt *build.Context, t *build.Tree, arg, importPath, dir string
if info.Package == "main" { if info.Package == "main" {
_, elem := filepath.Split(importPath) _, elem := filepath.Split(importPath)
targ = filepath.Join(t.BinDir(), elem) targ = filepath.Join(t.BinDir(), elem)
if ctxt.GOOS == "windows" {
targ += ".exe"
}
} else { } else {
targ = filepath.Join(t.PkgDir(), filepath.FromSlash(importPath)+".a") targ = filepath.Join(t.PkgDir(), filepath.FromSlash(importPath)+".a")
} }
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build darwin freebsd linux openbsd plan9
package mime package mime
import ( import (
......
...@@ -14,7 +14,7 @@ package cgo ...@@ -14,7 +14,7 @@ package cgo
#cgo linux LDFLAGS: -lpthread #cgo linux LDFLAGS: -lpthread
#cgo netbsd LDFLAGS: -lpthread #cgo netbsd LDFLAGS: -lpthread
#cgo openbsd LDFLAGS: -lpthread #cgo openbsd LDFLAGS: -lpthread
#cgo windows LDFLAGS: -lm -lmthreads #cgo windows LDFLAGS: -lm -mthreads
*/ */
import "C" import "C"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment