Skip to content

Conversation

2m
Copy link
Member

@2m 2m commented May 1, 2018

This PR proposes a discovery for Java home directories implemented by scanning well known system directories for available java installations. Currently this PR only addresses Linux and MacOS. // @cunei

This PR is a first step towards cross building across different Java versions.

Discovering Java home directories is also useful when a project needs to locate rt.jar of a different java installation (for example when working around -release 8 limitaions in JDK9)

@eed3si9n eed3si9n added the ready label May 1, 2018
@2m 2m mentioned this pull request May 1, 2018
Copy link
Member

@dwijnand dwijnand left a comment

Choose a reason for hiding this comment

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

Cool. Are these the initial pieces of your hackathon in Lisbon?

I left a few comments. Some less important than others.. 🙂

private final val JavaDiscoverConfigs = Seq(
JavaDiscoverConf("/usr/lib/jvm", "java-([0-9]+)-.*".r, ""), // most linux
JavaDiscoverConf("/Library/Java/JavaVirtualMachines",
"jdk[1\\.]*([0-9]+)\\..*".r,
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this match, for instance, /Library/Java/JavaVirtualMachines/jdk-10.jdk/?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I'll change it to jdk(1\.|-)?([0-9]+)\..* and extract the second group.

.collect {
case dir @ dirRexp(ver) => ver -> Paths.get(root, dir, inner).toFile
}
}(breakOut)
Copy link
Member

Choose a reason for hiding this comment

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

With breakOut making an exit in Scala 2.13, best implement this with .iterator (after JavaDiscoverConfigs) and a final .toMap

case JavaDiscoverConf(root, dirRexp, inner) =>
Option(new File(root).list())
.getOrElse(Array.empty[String])
.toSeq
Copy link
Member

Choose a reason for hiding this comment

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

Needn't be a seq, it can just be an iterator, with .iterator

@@ -268,6 +268,8 @@ object Keys {
val outputStrategy = settingKey[Option[sbt.OutputStrategy]]("Selects how to log output when running a main class.").withRank(DSetting)
val connectInput = settingKey[Boolean]("If true, connects standard input when running a main class forked.").withRank(CSetting)
val javaHome = settingKey[Option[File]]("Selects the Java installation used for compiling and forking. If None, uses the Java installation running the build.").withRank(ASetting)
val discoveredJavaHomes = settingKey[Map[String, File]]("Discovered Java home directories")
val additionalJavaHomes = settingKey[Map[String, File]]("Additional Java home directories")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe call this just javaHomes, like resolvers?

@@ -154,6 +161,8 @@ object Defaults extends BuildCommon {
scalaHome :== None,
apiURL := None,
javaHome :== None,
discoveredJavaHomes := discoverJavaHomes.value ++ additionalJavaHomes.value,
Copy link
Member

Choose a reason for hiding this comment

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

This definition isn't intuitive to me. Discovered should only be discovered.

Maybe there should be a fullJavaHomes (like fullResolvers and fullClasspath) with this RHS.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. Then fullJavaHomes = discoveredJavaHomes + javaHomes.

Copy link
Member

Choose a reason for hiding this comment

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

My vote is fullJavaHomes = discoveredJavaHomes + unmanagedJavaHomes as we had in Lisbon.

The reason is because the build user might want to either:

  1. append to the default list (like resolvers) or
  2. take over the entire mapping.

Note that for resolvers, we offer an entirely different mechanism ~/.sbt/repositories.

Copy link
Member

Choose a reason for hiding this comment

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

Why unmanagedJavaHomes over just javaHomes?

And if you prefer unmanagedJavaHomes, why discoveredJavaHomes instead of managedJavaHomes then?

Copy link
Member

Choose a reason for hiding this comment

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

Because javaHomes is not clear if it's the unmanaged part of the full.

javaHomes := Map("10" -> something)

I am ok with either discoveredJavaHomes or managedJavaHomes since the build user won't be interacting with them that much.

Copy link
Member

Choose a reason for hiding this comment

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

if you had to change it, that means sbt failed to detect some JDK homes automatically, and you are augmenting it with your machine-dependent info.

I see it as the same as resolvers. You're adding not-so-common custom java homes paths.

Also, like resolvers, it could have "doesn't work for everyone" concerns (e.g company-specific artefact repository URLs and company-specific JVM installation paths).

Copy link
Member

@eed3si9n eed3si9n May 1, 2018

Choose a reason for hiding this comment

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

resolvers typically go into build.sbt whereas your Global / (unmanaged)JavaHomes should go into user-defined global settings; never in build.sbt.

The users almost never have to debug fullResolvers whereas if JDK cross building doesn't work, the users will need to type show Global/fullJavaHomes to debug why it wasn't detected. This is analogous to managed vs unmanaged classpath or source. If there's a key named javaHomes already, wouldn't that be more confusing?

If we really want to use the name javaHomes, I would say we should get rid of fullJavaHomes, and just use javaHomes key where the user can just += into it.

Copy link
Member

Choose a reason for hiding this comment

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

resolvers typically go into build.sbt whereas your Global / (unmanaged)JavaHomes should go into user-defined global settings; never in build.sbt.

I can see both. For instance if you're in a company that uses a custom JDK/JVM installed (e.g at Twitter with GraalVM) in a specific but not-so-common path I can see it being experimented with at a project level, maybe eventually being defined in a company sbt plugin. Not necessarily in user-wide global settings.

And even if it were meant for user-wide global settings, why shouldn't it still be called javaHomes?

whereas if JDK cross building doesn't work, the users will need to type show Global/fullJavaHomes to debug why it wasn't detected.

To debug what was discovered they would look at discoveredJavaHomes.

If we really want to use the name javaHomes, I would say we should get rid of fullJavaHomes, and just use javaHomes key where the user can just += into it.

I don't follow, but I think it's non sequitur from my earlier comments.

Copy link
Member

Choose a reason for hiding this comment

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

things_and_cothings

Copy link
Member

Choose a reason for hiding this comment

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

We discussed in sbt 1 meeting today. I'll take this PR and make the edits.

@2m
Copy link
Member Author

2m commented May 1, 2018

Yes, this is from Lisbon and is a prequel to cross java home building. However it looks like this might be useful on its own as well.

@@ -1625,6 +1634,28 @@ object Defaults extends BuildCommon {
else old
}
))

private case class JavaDiscoverConf(root: String, dirRexp: Regex, innerDir: String)
Copy link
Member

Choose a reason for hiding this comment

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

These should move to CrossJava.scala, I think.

@eed3si9n eed3si9n mentioned this pull request May 3, 2018
@2m
Copy link
Member Author

2m commented May 3, 2018

Cntd. in #4139

@2m 2m closed this May 3, 2018
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.

3 participants