Skip to content

Commit

Permalink
Do not link targets in query
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Nov 9, 2023
1 parent 02f28b5 commit 8bcc5d2
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 102 deletions.
18 changes: 14 additions & 4 deletions bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func Boot(ctx context.Context, opts BootOpts) (Bootstrap, error) {
bs.Graph = g

{
opts := hbuiltin.Bootstrap(hbuiltin.Opts{
bopts := hbuiltin.Bootstrap(hbuiltin.Opts{
Pkgs: pkgs,
Root: root,
Config: cfg,
Expand All @@ -214,7 +214,7 @@ func Boot(ctx context.Context, opts BootOpts) (Bootstrap, error) {
})

for name, cfg := range cfg.BuildFiles.Roots {
opts := opts.Copy()
opts := bopts.Copy()

p, err := pkgs.FetchRoot(ctx, name, cfg)
if err != nil {
Expand All @@ -226,13 +226,23 @@ func Boot(ctx context.Context, opts BootOpts) (Bootstrap, error) {
Root: p,
})

err = buildfilesState.RunBuildFiles(ctx, opts)
files, err := buildfilesState.CollectFiles(ctx, opts.RootPkg.Root.Abs())
if err != nil {
return bs, err
}

err = buildfilesState.RunBuildFiles(ctx, files, opts)
if err != nil {
return bs, fmt.Errorf("buildfiles: root %v: %w", name, err)
}
}

err := buildfilesState.RunBuildFiles(ctx, opts)
files, err := buildfilesState.CollectFiles(ctx, bopts.RootPkg.Root.Abs())
if err != nil {
return bs, err
}

err = buildfilesState.RunBuildFiles(ctx, files, bopts)
if err != nil {
return bs, fmt.Errorf("buildfiles: %w", err)
}
Expand Down
41 changes: 37 additions & 4 deletions bootstrap/rrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,42 @@ func RunGen(ctx context.Context, e *scheduler.Scheduler, plain bool, filterFacto
return nil
}

func kindMatcher(gent *graph.Target) specs.KindMatcher {
gm := specs.KindMatcher{}
for _, m := range gent.Gen {
gm.Add(m)
}
return gm
}

func Query(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, plain bool) ([]specs.Target, error) {
mSimpl := m.Simplify()

err := RunGen(ctx, e, plain, func() (func(gent *graph.Target) bool, error) {
return func(gent *graph.Target) bool {
gm := kindMatcher(gent)

r := specs.Intersects(gm, mSimpl)

return r.Bool()
}, nil
})
if err != nil {
return nil, err
}

targets, err := e.Graph.Targets().Filter(m)
if err != nil {
return nil, err
}

targetSpecs := ads.Map(targets.Slice(), func(t *graph.Target) specs.Target {
return t.Target
})

return targetSpecs, nil
}

func GenerateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, targs []string, opts targetrun.RequestOpts, plain bool) (targetrun.Requests, error) {
if !e.Config.Engine.SmartGen {
if specs.IsMatcherExplicit(m) {
Expand Down Expand Up @@ -185,10 +221,7 @@ func GenerateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, t
}

return func(gent *graph.Target) bool {
gm := specs.KindMatcher{}
for _, m := range gent.Gen {
gm.Add(m)
}
gm := kindMatcher(gent)

r := specs.Intersects(gm, requiredMatchersSimpl)

Expand Down
16 changes: 8 additions & 8 deletions buildfiles/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
)

type RunOptions struct {
ThreadModifier func(thread *starlark.Thread, pkg *packages.Package)
UniverseFactory func() starlark.StringDict
CacheDirPath string
BuildHash func(hash.Hash)
Packages *packages.Registry
RootPkg *packages.Package
ThreadModifier func(thread *starlark.Thread, pkg *packages.Package)
UniverseFactory func() starlark.StringDict
CacheDirPath string
ProgramExtraHash func(hash.Hash)
Packages *packages.Registry
RootPkg *packages.Package
}

func (o RunOptions) Copy() RunOptions {
Expand Down Expand Up @@ -105,7 +105,7 @@ func (e *runContext) runBuildFilesForPackage(pkg *packages.Package, bc *breadcru

pkg.Globals = starlark.StringDict{}
for _, file := range pkg.SourceFiles {
globals, err := e.RunBuildFile(pkg, file.Path, nbc)
globals, err := e.RunBuildFile(pkg, file, nbc)
if err != nil {
return err
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func (e *runContext) buildProgram(path string, universe starlark.StringDict) (*s
h.I64(info.ModTime().Unix())
h.UI32(uint32(info.Mode().Perm()))
h.String(utils.Version)
e.BuildHash(h)
e.ProgramExtraHash(h)

sum := h.Sum()

Expand Down
19 changes: 6 additions & 13 deletions buildfiles/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type State struct {
Patterns []string
Ignore []string
Packages *packages.Registry
files []*packages.SourceFile
files []string

cacheRunBuildFileCache maps.Map[string, starlark.StringDict]
cacheRunBuildFileLocks maps.Map[string, *sync.Mutex]
Expand All @@ -37,11 +37,11 @@ func NewState(s State) *State {
return &s
}

func (s *State) CollectFiles(ctx context.Context, root string) (packages.SourceFiles, error) {
func (s *State) CollectFiles(ctx context.Context, root string) ([]string, error) {
done := log.TraceTimingDone("RunBuildFiles:walk")
defer done()

files := make(packages.SourceFiles, 0)
files := make([]string, 0)

err := xfs.StarWalk(ctx, root, s.Patterns[0], s.Ignore, func(path string, d fs.DirEntry, err error) error {
if err := ctx.Err(); err != nil {
Expand All @@ -52,9 +52,7 @@ func (s *State) CollectFiles(ctx context.Context, root string) (packages.SourceF
return nil
}

files = append(files, &packages.SourceFile{
Path: filepath.Join(root, path),
})
files = append(files, filepath.Join(root, path))
return nil
})
if err != nil {
Expand All @@ -64,18 +62,13 @@ func (s *State) CollectFiles(ctx context.Context, root string) (packages.SourceF
return files, nil
}

func (s *State) RunBuildFiles(ctx context.Context, options RunOptions) error {
func (s *State) RunBuildFiles(ctx context.Context, files []string, options RunOptions) error {
rootPkg := options.RootPkg
rootAbs := rootPkg.Root.Abs()

files, err := s.CollectFiles(ctx, rootAbs)
if err != nil {
return err
}

pkgs := make([]*packages.Package, 0, len(files))
for _, file := range files {
dir := filepath.Dir(file.Path)
dir := filepath.Dir(file)

relRoot, err := filepath.Rel(rootAbs, dir)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions cmd/heph/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package main
import (
"cmp"
"github.com/hephbuild/heph/cmd/heph/search"
"github.com/hephbuild/heph/graph"
"github.com/hephbuild/heph/specs"
"github.com/hephbuild/heph/utils/ads"
"github.com/lithammer/fuzzysearch/fuzzy"
"golang.org/x/exp/slices"
"strings"
)

func sortedTargets(targets []*graph.Target, skipPrivate bool) []*graph.Target {
stargets := make([]*graph.Target, 0)
func sortedTargets(targets []specs.Target, skipPrivate bool) []specs.Target {
stargets := make([]specs.Target, 0)
for _, target := range targets {
if skipPrivate && target.IsPrivate() {
continue
Expand All @@ -21,14 +20,14 @@ func sortedTargets(targets []*graph.Target, skipPrivate bool) []*graph.Target {
stargets = append(stargets, target)
}

slices.SortFunc(stargets, func(a, b *graph.Target) int {
ads.SortP(stargets, func(a, b *specs.Target) int {
return strings.Compare(a.Addr, b.Addr)
})

return stargets
}

func sortedTargetNames(targets []*graph.Target, skipPrivate bool) []string {
func sortedTargetNames(targets []specs.Target, skipPrivate bool) []string {
names := make([]string, 0)
for _, t := range sortedTargets(targets, skipPrivate) {
names = append(names, t.Addr)
Expand Down
4 changes: 4 additions & 0 deletions cmd/heph/parse_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ func parseTargetsAndArgsWithScheduler(ctx context.Context, e *scheduler.Schedule
func generateRRs(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher, targs []string) (targetrun.Requests, error) {
return bootstrap.GenerateRRs(ctx, e, m, targs, getRROpts(), *plain)
}

func query(ctx context.Context, e *scheduler.Scheduler, m specs.Matcher) ([]specs.Target, error) {
return bootstrap.Query(ctx, e, m, *plain)
}
26 changes: 13 additions & 13 deletions cmd/heph/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,30 @@ var queryCmd = &cobra.Command{
matcher = specs.AndNodeFactory(matcher, includeExcludeMatcher)
}

_, err = generateRRs(ctx, bs.Scheduler, matcher, nil)
if err != nil {
return err
}

selected, err := bs.Graph.Targets().Filter(matcher)
selected, err := query(ctx, bs.Scheduler, matcher)
if err != nil {
return err
}

if jsonOutput {
selected := selected.Slice()

for _, target := range selected {
err := exprs.ExecDeep(&target.Annotations, map[string]exprs.Func{
funcs := map[string]exprs.Func{
"addr": func(expr exprs.Expr) (string, error) {
return target.Addr, nil
},
"sandbox_root": func(expr exprs.Expr) (string, error) {
return bs.Scheduler.Runner.SandboxTreeRoot(target).Abs(), nil
},
})
}

err := exprs.ExecDeep(&target.Doc, funcs)
if err != nil {
return err
return fmt.Errorf("doc: %w", err)
}

err = exprs.ExecDeep(&target.Annotations, funcs)
if err != nil {
return fmt.Errorf("annotations: %w", err)
}
}

Expand All @@ -182,11 +182,11 @@ var queryCmd = &cobra.Command{
return nil
}

if selected.Len() == 0 {
if len(selected) == 0 {
return nil
}

fmt.Println(strings.Join(sortedTargetNames(selected.Slice(), false), "\n"))
fmt.Println(strings.Join(sortedTargetNames(selected, false), "\n"))
return nil
},
}
Expand Down
8 changes: 1 addition & 7 deletions cmd/heph/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"github.com/hephbuild/heph/buildfiles"
"github.com/hephbuild/heph/config"
"github.com/hephbuild/heph/log/log"
"github.com/hephbuild/heph/packages"
"github.com/hephbuild/heph/specs"
"github.com/hephbuild/heph/targetrun"
"github.com/hephbuild/heph/utils"
"github.com/hephbuild/heph/utils/ads"
"github.com/hephbuild/heph/utils/finalizers"
"github.com/hephbuild/heph/utils/xstarlark"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -336,14 +334,10 @@ var fmtCmd = &cobra.Command{

var files []string
if len(args) == 0 {
cfiles, err := buildfilesState.CollectFiles(ctx, bs.Root.Root.Abs())
files, err = buildfilesState.CollectFiles(ctx, bs.Root.Root.Abs())
if err != nil {
return err
}

files = ads.Map(cfiles, func(f *packages.SourceFile) string {
return f.Path
})
} else {
files = args
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/heph/searchui2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (m model) details() string {

sb.WriteString(styleTitle.Render(t.Addr))
sb.WriteString("\n")
if len(t.Source) > 0 {
if len(t.Sources) > 0 {
sb.WriteString(styleLocation.Render(t.SourceFile()))
sb.WriteString("\n")
}
Expand Down
4 changes: 3 additions & 1 deletion graph/link_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ func (e *State) Register(spec specs.Target) error {

if t := e.targets.FindT(spec); t != nil {
if !t.Spec().Equal(spec) {
return fmt.Errorf("%v is already declared and does not equal the one defined in %v\n%s\n\n%s", spec.Addr, t.Source, t.Json(), spec.Json())
return fmt.Errorf("%v is already declared and does not equal the one defined in %v\n%s\n\n%s", spec.Addr, t.Sources[0].SourceFile(), t.Json(), spec.Json())
}

t.Sources = append(t.Sources, spec.Sources...)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion graphprint/graphprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Print(w io.Writer, target *graph.Target, transitive bool) {
}

fmt.Fprintln(w, "Source:")
for _, s := range target.Source {
for _, s := range target.Sources[0].CallFrames {
fmt.Fprintln(w, indent, s.String())
}

Expand Down
2 changes: 1 addition & 1 deletion hbuiltin/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Bootstrap(opts Opts) buildfiles.RunOptions {
return universe
},
CacheDirPath: opts.Root.Home.Join("__BUILD").Abs(),
BuildHash: func(h hash.Hash) {
ProgramExtraHash: func(h hash.Hash) {
h.String(predeclaredHash)
},
Packages: opts.Pkgs,
Expand Down
Loading

0 comments on commit 8bcc5d2

Please sign in to comment.