Skip to content

Add (includes ((sub_dirs (...)))) stanza - #35

Closed
bobot wants to merge 6 commits into
ocaml:masterfrom
bobot:add-includes
Closed

bobot wants to merge 6 commits into
ocaml:masterfrom
bobot:add-includes

Conversation

@bobot

@bobot bobot commented Mar 22, 2017 •

Copy link
Copy Markdown
Contributor

Fix #34

The .ml and .mli filenames are not kept as string but as Path.t

  • : add -I
  • : generalize the lookup of files for c files.

@ghost

ghost commented Mar 23, 2017

Copy link
Copy Markdown

I'm a bit worried by the implication of this patch in terms of maintainability. I don't know how it's going to interact when we start switching to proper namespaces either.

There is another solution that gives the same power to the user and I think would fit better in the current design: instead of looking for modules in arbitrary directories, we could simply bring them to the directory of the library. You could do this right now:

(rule
 ((targets (foo.ml))
   (deps    (subdir/foo.ml))
   (action  (copy_and_add_line_directive ${<} ${@}))))

It is painful if you have to setup rules for 140 files, but we could add something to make it easier:

(copy_files_here_and_add_line_directives (subdir/*.ml))

There are a few things to be careful about, but it should be less invasive and should cover more cases.

@bobot

bobot commented Mar 23, 2017 •

Copy link
Copy Markdown
Contributor Author

I'm a bit worried by the implication of this patch in terms of maintainability. I don't know how it's going to interact when we start switching to proper namespaces either.

I don't know what are proper namespaces (Leo's ideas?), so it could indeed interact badly. But I though that the main question was more after installation (.cmi, .cmo) than before.

we could simply bring them to the directory of the library

It's an interesting solution and it explicits the implicit intent of the proposition "consider all the directories as one". But it seems to lost too much information. For example the configuration of the source directory of merlin Sneeds this information. Does an implementation with a virtualized/proxy view of the file system would be okay for you? I think that will move the feature one level down.

Many project use directory to order files even if they are in the same namespace, why3 is another example.

@bobot

bobot commented Mar 23, 2017 •

Copy link
Copy Markdown
Contributor Author

a virtualized/proxy view of the file system

It is just a generalization of sources_and_targets_known_so_far and known_targets_by_src_dir_so_far where some directories are merged into one.

@ghost

ghost commented Mar 23, 2017

Copy link
Copy Markdown

I don't know what are proper namespaces (Leo's ideas?), so it could indeed interact badly. But I though that the main question was more after installation (.cmi, .cmo) than before.

Yeah that's @lpw25 proposal

I'm still not convinced we need to add more special concepts to support this case now. Some time ago we setup the build of the OCaml compiler with jbuilder for some specific usage. The compiler does the same thing and yet it was trivial to setup the build with jbuilder, even before (wrapped false) was supported.

If you can convert a library split into several directories into several libraries, this means that the various sub-directories are closed, and you gain nothing by trying to regroup everything into the same build stanza. I agree that the need to add these opens is a bit annoying, but hopefully this is something that can be improved in the future when we get proper namespaces. Moreover there are simple workarounds in the meantime.

For instance you can add an import.ml file in each sub-directory that include all the other libraries:

include Frama_c_foo
include Frama_c_bar
...

and then you open this module in every file, either manually or with -open Import. This is something we do all the time at Jane Street and it works well in practice.

@bobot

bobot commented Mar 27, 2017

Copy link
Copy Markdown
Contributor Author

I think I want to far in this branch, sorry, I will create new MR but perhaps you can already tell me if you think these modifications are interesting:

  • jbuild-ignore really ignore directories, currently that only ignore the jbuild file inside these directory. I think it is more useful for the user this way (big directory, directory without read permission, ...). The current semantic seems only useful for the test of jbuilder itself. So I add to run them differently. The modification is done directly in File_tree
  • Remove warning about colors from ocaml compiler program output. I think for 4.02.3.
  • Print more descriptive message for Compiling module foo instead of the current full command line, allows to change the verbosity on the command line
  • Add in .merlin the source of internal library S myinternallib, otherwise C-l does the lookup in _build
  • I removed the warning for .mli -> .ml copying, it is just temporary, I need to find a better way.

Now the collapsing is done directly in File_tree so it is transparent for most of jbuilder, only the copying rules and merlin add to be modified. However there is still a problem with the error output File "..." ... : and the goto-error of emacs but it is already not working well (goes to _build/...).

@ghost

ghost commented Mar 27, 2017

Copy link
Copy Markdown

I think I want to far in this branch, sorry, I will create new MR but perhaps you can already tell me if you think these modifications are interesting:

Sure. I didn't read the patch in this feature, so I just commenting on the feature themselves. They should be split into several PRs so that review is easier.

  • jbuild-ignore really ignore directories, currently that only ignore the jbuild file inside these directory. I think it is more useful for the user this way (big directory, directory without read permission, ...). The current semantic seems only useful for the test of jbuilder itself. So I add to run them differently. The modification is done directly in File_tree

I'd have to see the PR to understand better what it means. (file_recursively_in ...) is used in re2 as well and is convenient to sandbox an external build system. Perhaps the file tree could be changed so that some parts could be built lazily? That way if an ignored sub-tree is not readable then you get an error only if you try to depend on it.

  • Remove warning about colors from ocaml compiler program output. I think for 4.02.3.
  • Print more descriptive message for Compiling module foo instead of the current full command line, allows to change the verbosity on the command line

This looks good to me, I welcome these features!

  • Add in .merlin the source of internal library S myinternallib, otherwise C-l does the lookup in _build

I'm not sure I follow this change, I'd have to see the patch

Now the collapsing is done directly in File_tree so it is transparent for most of jbuilder, only the copying rules and merlin add to be modified. However there is still a problem with the error output File "..." ... : and the goto-error of emacs but it is already not working well (goes to _build/...).

Before doing anymore work in this direction, can you try the method I described to see what are the limitations? I'm not ready to commit into maintaining a complex feature like this, especially if it's not future proof and reasonable workarounds exist.

@bobot

bobot commented Mar 27, 2017 •

Copy link
Copy Markdown
Contributor Author

They should be split into several PRs so that review is easier.

Ok I will do that.

Before doing anymore work in this direction, can you try the method I described to see what are the limitations? I'm not ready to commit into maintaining a complex feature like this, especially if it's not future proof and reasonable workarounds exist.

Ok.

@ghost

ghost commented Apr 18, 2017

Copy link
Copy Markdown

I gave a quick try to the solution of adding rules to copy the .ml/.mli and adding line directives. I did this manually by writing some (rule ...) stanzas. Everything seems to work fine, even for .ml generated in sub-directories by another jbuild file. The only hack I had to do is add the right S ... line in the generated .merlin file so that merlin would find the right .ml file.

If you are still interested in this, this seem to me like the most straightforward solution. Moreover it will work out of the box when we have the bridge with jenga.

Technically, we would need a stanza like this:

(copy_modules_of a/b/c)

which would setup the copy rules and add the right lines to the .merlin.

@bobot

bobot commented Apr 18, 2017

Copy link
Copy Markdown
Contributor Author

Thank you for looking into it yourself.

However there is still a problem with the error output File "..." ... : and the goto-error of emacs but it is already not working well (goes to _build/...).

In case of error in the ml file do you go to the right file ? Should the copy rule add a preprocessor pragma ? Or should the filenames be converted back in the command output ?

@ghost

ghost commented Apr 18, 2017

Copy link
Copy Markdown

In case of error in the ml file do you go to the right file ?

yes

Should the copy rule add a preprocessor pragma ? Or should the filenames be converted back in the command output ?

The copy rule should add a pragma. This is the jbuild I used for the experiment:

(library
 ((name plop)
  ))

(rule
 ((targets (bidule.mli))
  (deps    (a/b/c/bidule.mli))
  (action  (copy-and-add-line-directive ${<} ${@}))))

(rule
 ((targets (bidule.ml))
  (deps    (a/b/c/bidule.ml))
  (action  (copy-and-add-line-directive ${<} ${@}))))

(rule
 ((targets (lex.ml))
  (deps    (a/b/c/lex.ml))
  (action  (copy-and-add-line-directive ${<} ${@}))))

@ghost ghost mentioned this pull request Apr 18, 2017
@bobot bobot closed this May 16, 2017
@bobot bobot reopened this May 17, 2017
@bobot

bobot commented May 17, 2017

Copy link
Copy Markdown
Contributor Author

Sorry for the intermediate closing (I pushed a reset of master branch...). The branch now contains an implementation of your proposition. Except perhaps it copies and add preprocessing information to all the files not just ocaml modules. I think it is better to not single out *.ml* because why not *.mll *.mly, ... I think the preprocessing information is universal enough but perhaps I should add a way to change how preprocessing information is added depending on the file extension in another MR.

The name of the new rule should perhaps be changed in copy_files_of ...?

@ghost

ghost commented May 17, 2017

Copy link
Copy Markdown

I initially suggested to copy only the .ml/.mli files for the following reasons:

  • to force having the rules to generate source files in the satellites directories, which seems more natural to me. For instance if you have a/b/foo.mll, IMO it's better to put (ocamllex (foo)) in a/b/jbuild
  • to catch cases where the .mli for a module is in a directory and the .ml in another one, which are surely errors. I'm sure there are cases where it's useful for some reason, but then the user should write the explicit rules, to avoid surprises
  • so that we don't have to think about whether to add a line directive or not
  • for merlin to work in the directories we are copying from, it needs to be descendant of the destination. Making this mandatory for copy_modules_of seems fine, but a bit arbitrary for a fully generic version

I don't mind too much going the route of copying everything, but we should be consistent with the forms available in actions for copying files. Currently there are two forms available:

  • (copy <src> <dst>)
  • (copy-and-add-line-directive <src> <dst>)

So we should be consistent with that. We could have two different stanzas that take globs:

(copy_files a/b/*.bin)
(copy_files_and_add_lines_directives a/b/*.{ml,mli})

Regarding the implementation, there is currently an unwritten invariant about sources_and_targets_known_so_far. It assumes that the rules for the files we are interested in have already been setup. copy_files/copy_modules_of implicitly creates dependencies between directories and we have to evaluate the rules in a certain order. One solution would be to top-sort the list of directories.

However, if we reject source directories that aren't descendant, then we can just make sure that we visit the children before the parents.

@bobot

bobot commented May 22, 2017

Copy link
Copy Markdown
Contributor Author

So we should be consistent with that. We could have two different stanzas that take globs copy_files/copy_files_and_add_lines_directives

Ok, I'm going to do that.

However, if we reject source directories that aren't descendant, then we can just make sure that we visit the children before the parents.

I think jbuild work well because you have to do things a certain way, I don't want to push it more out of its track 🛤️ I think it is a good and natural property to have.

@ghost

ghost commented May 22, 2017

Copy link
Copy Markdown

Cool, BTW copy_files_and_lines_directives is a really long name. We could use a shorter name such as copy_files#, and add the corresponding copy# action.

@bobot

bobot commented May 22, 2017

Copy link
Copy Markdown
Contributor Author

Currently jbuilder only support globbing files in a single directory.

I think I will not relax this limitation for copy-files for the first version.

@ghost

ghost commented May 22, 2017

Copy link
Copy Markdown

I also have a plan to build jbuilder projects with jenga, and jenga has the same limitation. So I'd rather keep it for now

@bobot

bobot commented May 22, 2017

Copy link
Copy Markdown
Contributor Author

Why "copy-..." in action and "copy_" in rule name?

@ghost

ghost commented May 22, 2017

Copy link
Copy Markdown

I guess because except for actions everything uses underscores. But I don't really mind, if you prefer copy-files, feel free to use that.

The reason for using underscores in some places and dashes in other is more historical than anything else. Initially most of the jbuild syntax was imported from our jenga-rules where the parser are generated by ppx_sexp_conv which dictates the use of underscore. I made up the action DSL at some point during the development of jbuilder and I thought dashes were nicer since it's what program names usually contain.

@bobot

bobot commented May 24, 2017

Copy link
Copy Markdown
Contributor Author

Even if - is indeed prettier I didn't make the renaming, I think it should be done for all the stanza at once.

Can you take a new look at this MR, thank you.

@bobot bobot changed the title [WIP] Add (includes ((sub_dirs (...)))) stanza Add (includes ((sub_dirs (...)))) stanza May 24, 2017
Comment thread doc/jbuild.rst Outdated
----------

The ``copy_files`` and ``copy_files_and_add_lines_directives`` stanza
allows to specify that files in another directory could be copied to

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be "from another directory"? Also why "could" rather than "will"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"could" because if the file is not needed it will not be copied. but I will precise and rephrase.

Comment thread doc/jbuild.rst Outdated
copy_files
----------

The ``copy_files`` and ``copy_files_and_add_lines_directives`` stanza

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stanza --> stanzas

Comment thread doc/jbuild.rst Outdated

The ``copy_files`` and ``copy_files_and_add_lines_directives`` stanza
allows to specify that files in another directory could be copied to
the current directory. The stanza ``copy_files#`` is a shortcut for

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to: You can use copy_files# as a shorthand for ...

Comment thread doc/jbuild.rst Outdated

(copy_files <glob>)

``<glob>`` represent the set of files matched, see the :ref:`glob

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--> "represents the set of files to copy"

Comment thread doc/jbuild.rst Outdated
(copy_files <glob>)

``<glob>`` represent the set of files matched, see the :ref:`glob
<glob>` for details

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing dot at the end.

Comment thread src/gen_rules.ml Outdated

let copy_files_rules (def: Copy_files.t) ~src_dir ~dir ~package_context:_ =
let src_glob = SC.expand_vars sctx ~dir def.glob in
let glob_in_src = Path.relative src_dir src_glob in

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't enforce that the path must be a sub-directory of the current directory. You could write this instead:

Path.append src_dir (Path.relative Path.root src_glob)

and print a better error when Path.relative Path.root src_glob fails. Please capture the location of the glob as well so that the error is properly located.

BTW, there are many variables defined here and src_glob is used only once. I think it'd be easier to read if it was scoped accordingly:

let glob_in_src =
  let src_glob = ... in
  ...
in

Comment thread src/gen_rules.ml Outdated
die "invalid glob in %s/jbuild: %s" (Path.to_string src_dir) msg
in
(* add rules *)
let modules = SC.sources_and_targets_known_so_far sctx ~src_path:src_in_src in

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modules --> files

Comment thread src/gen_rules.ml Outdated
(file_src,file_dst)
else Action.Mini_shexp.Ast.Copy (file_src,file_dst)
in
(Build.action

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already Build.copy. Let's add Build.copy_and_add_line_directive as well. This will make this code less verbose.

Comment thread src/gen_rules.ml Outdated
let () =
(* Goes first in subdirectories *)
let subtree_smaller x y =
- (Path.compare x.SC.Dir_with_jbuild.src_dir y.SC.Dir_with_jbuild.src_dir) in

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compare b a instead of - (compare a b)?

Comment thread test/workspaces/copy_files/jbuild Outdated
@@ -0,0 +1,8 @@
(jbuild_version 1)

(ocamllex (lexer1))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems nicer to me if this stanza is next to the .mll. I think we should move it lexers/jbuild and change the glob to lexers/*.{ml,mli}

@ghost

ghost commented May 26, 2017

Copy link
Copy Markdown

Alright, I tried this on jbuilder itself; I wanted to see if merging the vendored libraries into the main one would increase parallelism.

The main issue I had is that I initially made a few typos while typing the globs and as a result nothing was happening, I was just getting errors about unknown modules, which is not very helpful. I think a glob that match nothing should be an error.

@ghost

ghost commented May 26, 2017

Copy link
Copy Markdown

BTW, @mshinwell says that you should finish your OCaml PRs 😛

@bobot

bobot commented May 28, 2017

Copy link
Copy Markdown
Contributor Author

(Oh! I completely missed the new comments in them..... sorry. next week)

@ghost ghost mentioned this pull request May 30, 2017
@bobot

bobot commented Jun 2, 2017

Copy link
Copy Markdown
Contributor Author

All the fixes have been done.

@ghost

ghost commented Jun 2, 2017

Copy link
Copy Markdown

Thanks, I want to release the 1.0.0 next week. I'll probably review non-bugfix PRs after that.

@ghost ghost mentioned this pull request Jul 27, 2017
ghost pushed a commit that referenced this pull request Sep 10, 2017
Add (copy_files <glob>) and (copy_files# <glob>) stanzas. These
stanzas setup rules for copying files from a sub-directory to the
current directory.

This provides a reasonable way to support multi-directory
library/executables in jbuilder.
@ghost

ghost commented Sep 10, 2017

Copy link
Copy Markdown

Ok, as discussed yesterday during the design meeting, I merged this PR. I rebased and merged it manually. I got rid of copy_files_and_add_line_directives and kept only copy_files#, since at least the doc doesn't mention the old copy_and_add_line_directive action anymore.

@ghost ghost closed this Sep 10, 2017
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant