@@ -8,11 +8,6 @@ package work
8
8
9
9
import (
10
10
"bytes"
11
- "cmd/go/internal/base"
12
- "cmd/go/internal/cache"
13
- "cmd/go/internal/cfg"
14
- "cmd/go/internal/load"
15
- "cmd/go/internal/str"
16
11
"encoding/json"
17
12
"errors"
18
13
"fmt"
@@ -30,6 +25,12 @@ import (
30
25
"strings"
31
26
"sync"
32
27
"time"
28
+
29
+ "cmd/go/internal/base"
30
+ "cmd/go/internal/cache"
31
+ "cmd/go/internal/cfg"
32
+ "cmd/go/internal/load"
33
+ "cmd/go/internal/str"
33
34
)
34
35
35
36
// actionList returns the list of actions in the dag rooted at root
@@ -490,6 +491,10 @@ func (b *Builder) build(a *Action) (err error) {
490
491
return nil
491
492
}
492
493
494
+ if err := allowInstall (a ); err != nil {
495
+ return err
496
+ }
497
+
493
498
// make target directory
494
499
dir , _ := filepath .Split (a .Target )
495
500
if dir != "" {
@@ -1192,6 +1197,10 @@ func (b *Builder) link(a *Action) (err error) {
1192
1197
return err
1193
1198
}
1194
1199
1200
+ if err := allowInstall (a ); err != nil {
1201
+ return err
1202
+ }
1203
+
1195
1204
// make target directory
1196
1205
dir , _ := filepath .Split (a .Target )
1197
1206
if dir != "" {
@@ -1366,6 +1375,10 @@ func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string,
1366
1375
}
1367
1376
1368
1377
func (b * Builder ) installShlibname (a * Action ) error {
1378
+ if err := allowInstall (a ); err != nil {
1379
+ return err
1380
+ }
1381
+
1369
1382
// TODO: BuildN
1370
1383
a1 := a .Deps [0 ]
1371
1384
err := ioutil .WriteFile (a .Target , []byte (filepath .Base (a1 .Target )+ "\n " ), 0666 )
@@ -1416,6 +1429,10 @@ func (b *Builder) linkShared(a *Action) (err error) {
1416
1429
}
1417
1430
defer b .flushOutput (a )
1418
1431
1432
+ if err := allowInstall (a ); err != nil {
1433
+ return err
1434
+ }
1435
+
1419
1436
if err := b .Mkdir (a .Objdir ); err != nil {
1420
1437
return err
1421
1438
}
@@ -1481,8 +1498,12 @@ func BuildInstallFunc(b *Builder, a *Action) (err error) {
1481
1498
// advertise it by touching the mtimes (usually the libraries are up
1482
1499
// to date).
1483
1500
if ! a .buggyInstall && ! b .IsCmdList {
1484
- now := time .Now ()
1485
- os .Chtimes (a .Target , now , now )
1501
+ if cfg .BuildN {
1502
+ b .Showcmd ("" , "touch %s" , a .Target )
1503
+ } else if err := allowInstall (a ); err == nil {
1504
+ now := time .Now ()
1505
+ os .Chtimes (a .Target , now , now )
1506
+ }
1486
1507
}
1487
1508
return nil
1488
1509
}
@@ -1493,6 +1514,9 @@ func BuildInstallFunc(b *Builder, a *Action) (err error) {
1493
1514
a .built = a1 .built
1494
1515
return nil
1495
1516
}
1517
+ if err := allowInstall (a ); err != nil {
1518
+ return err
1519
+ }
1496
1520
1497
1521
if err := b .Mkdir (a .Objdir ); err != nil {
1498
1522
return err
@@ -1522,6 +1546,13 @@ func BuildInstallFunc(b *Builder, a *Action) (err error) {
1522
1546
return b .moveOrCopyFile (a .Target , a1 .built , perm , false )
1523
1547
}
1524
1548
1549
+ // allowInstall returns a non-nil error if this invocation of the go command is
1550
+ // allowed to install a.Target.
1551
+ //
1552
+ // (The build of cmd/go running under its own test is forbidden from installing
1553
+ // to its original GOROOT.)
1554
+ var allowInstall = func (* Action ) error { return nil }
1555
+
1525
1556
// cleanup removes a's object dir to keep the amount of
1526
1557
// on-disk garbage down in a large build. On an operating system
1527
1558
// with aggressive buffering, cleaning incrementally like
@@ -1685,6 +1716,10 @@ func (b *Builder) installHeader(a *Action) error {
1685
1716
return nil
1686
1717
}
1687
1718
1719
+ if err := allowInstall (a ); err != nil {
1720
+ return err
1721
+ }
1722
+
1688
1723
dir , _ := filepath .Split (a .Target )
1689
1724
if dir != "" {
1690
1725
if err := b .Mkdir (dir ); err != nil {
0 commit comments