• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Wuffs Authors.
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//    https://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
15package wuffsroot
16
17import (
18	"errors"
19	"go/build"
20	"os"
21	"path/filepath"
22	"sync"
23)
24
25var cache struct {
26	mu    sync.Mutex
27	value string
28}
29
30func Value() (string, error) {
31	cache.mu.Lock()
32	value := cache.value
33	cache.mu.Unlock()
34
35	if value != "" {
36		return value, nil
37	}
38
39	// TODO: look for a WUFFSROOT environment variable? Also check that
40	// wuffs-root-directory.txt exists?
41	for _, p := range filepath.SplitList(build.Default.GOPATH) {
42		p = filepath.Join(p, "src", "github.com", "google", "wuffs")
43		if o, err := os.Stat(p); err == nil && o.IsDir() {
44			cache.mu.Lock()
45			cache.value = p
46			cache.mu.Unlock()
47
48			return p, nil
49		}
50	}
51	return "", errors.New("could not find Wuffs root directory")
52}
53