Skip to content

Commit

Permalink
Mill support for Chisel3 (#1035)
Browse files Browse the repository at this point in the history
Co-Authored-By: Jack Koenig <jack.koenig3@gmail.com>
Co-Authored-By: Jim Lawson <ucbjrl@berkeley.edu>
  • Loading branch information
3 people committed Mar 20, 2019
1 parent 2c449c5 commit 1894319
Show file tree
Hide file tree
Showing 4 changed files with 388 additions and 31 deletions.
94 changes: 94 additions & 0 deletions CommonBuild.sc
@@ -0,0 +1,94 @@
import ammonite.ops._
import mill._
import mill.scalalib._

def scalacOptionsVersion(scalaVersion: String): Seq[String] = {
Seq() ++ {
// If we're building with Scala > 2.11, enable the compile option
// switch to support our anonymous Bundle definitions:
// https://github.com/scala/bug/issues/10047
if (scalaVersion.startsWith("2.11.")) {
Seq()
} else {
Seq(
"-Xsource:2.11",
"-Ywarn-unused:imports",
"-Ywarn-unused:locals"
)
}
}
}

def javacOptionsVersion(scalaVersion: String): Seq[String] = {
Seq() ++ {
// Scala 2.12 requires Java 8. We continue to generate
// Java 7 compatible code for Scala 2.11
// for compatibility with old clients.
if (scalaVersion.startsWith("2.11.")) {
Seq("-source", "1.7", "-target", "1.7")
} else {
Seq("-source", "1.8", "-target", "1.8")
}
}
}

// Define our own BuildInfo since mill doesn't currently have one.
trait BuildInfo extends ScalaModule { outer =>

def buildInfoObjectName: String = "BuildInfo"

def buildInfoMembers: T[Map[String, String]] = T {
Map.empty[String, String]
}

private def generateBuildInfo(outputPath: Path, members: Map[String, String]) = {
val outputFile = outputPath / "BuildInfo.scala"
val packageName = members.getOrElse("buildInfoPackage", "")
val packageDef = if (packageName != "") {
s"package ${packageName}"
} else {
""
}
val internalMembers =
members
.map {
case (name, value) => s""" val ${name}: String = "${value}""""
}
.mkString("\n")
write(outputFile,
s"""
|${packageDef}
|case object ${buildInfoObjectName}{
|$internalMembers
| override val toString: String = {
| "buildInfoPackage: %s, version: %s, scalaVersion: %s" format (
| buildInfoPackage, version, scalaVersion
| )
| }
|}
""".stripMargin)
outputPath
}

override def generatedSources = T {
super.generatedSources() :+ PathRef(generateBuildInfo(T.ctx().dest, buildInfoMembers()))
}
}

// Define some file filters to exclude unwanted files from created jars.
type JarFileFilter = (Path, RelPath) => Boolean
// Exclude any `.DS_Store` files
val noDS_StoreFiles: JarFileFilter = (p: Path, relPath: RelPath) => {
relPath.last != ".DS_Store"
}

// Exclude non-source files - accept all resource files, but only *.{java,scala} from source paths
val onlySourceFiles: JarFileFilter = (p: Path, relPath: RelPath) => {
p.last == "resources" || (relPath.ext == "scala" || relPath.ext == "java")
}

// Apply a sequence of file filters - only accept files which satisfy all filters.
// We expect this to be curried, the resulting file filter passed to createJar()
def forallFilters(fileFilters: Seq[JarFileFilter])(p: Path, relPath: RelPath): Boolean = {
fileFilters.forall(f => f(p, relPath))
}
78 changes: 47 additions & 31 deletions Makefile
@@ -1,31 +1,35 @@
# Retain all intermediate files.
.SECONDARY:

SBT ?= sbt
SBT_FLAGS ?= -Dsbt.log.noformat=true
MKDIR ?= mkdir -p
CURL ?= curl -L
MILL_BIN ?= $(HOME)/bin/mill
MILL ?= $(MILL_BIN) --color false
MILL_REMOTE_RELEASE ?= https://github.com/lihaoyi/mill/releases/download/0.3.5/0.3.5

# Fetch mill (if we don't have it).
$(MILL_BIN):
$(MKDIR) $(dir $@)
@echo $(CURL) --silent --output $@.curl --write-out "%{http_code}" $(MILL_REMOTE_RELEASE)
STATUSCODE=$(shell $(CURL) --silent --output $@.curl --write-out "%{http_code}" $(MILL_REMOTE_RELEASE)) && \
if test $$STATUSCODE -eq 200; then \
mv $@.curl $@ && chmod +x $@ ;\
else \
echo "Can't fetch $(MILL_REMOTE_RELEASE)" && cat $@.curl && echo ;\
false ;\
fi

mill-tools: $(MILL_BIN)

CHISEL_VERSION = $(shell "$(SBT)" $(SBT_FLAGS) "show version" | tail -n 1 | cut -d ' ' -f 2)

SRC_DIR ?= .
CHISEL_BIN ?= $(abspath $(SRC_DIR)/bin)
export CHISEL_BIN

#$(info Build Chisel $(CHISEL_VERSION))

# The targetDir will be rm -rf'ed when "make clean"
targetDir ?= ./generated
# The TEST_OUTPUT_DIR will be rm -rf'ed when "make clean"
TEST_OUTPUT_DIR ?= ./test-outputs
RM_DIRS := $(TEST_OUTPUT_DIR) test-reports $(targetDir)
#CLEAN_DIRS := doc

test_src_dir := src/test/scala/ChiselTests
test_results := $(filter-out main DirChange Pads SIntOps,$(notdir $(basename $(wildcard $(test_src_dir)/*.scala))))
c_resources_dir := src/main/resources

test_outs := $(addprefix $(targetDir)/, $(addsuffix .out, $(test_results)))
TEST_OUTPUT_DIR ?= ./test_run_dur
RM_DIRS := $(TEST_OUTPUT_DIR)

.PHONY: smoke publish-local pubishLocal check clean jenkins-build coverage scaladoc test checkstyle compile
.PHONY: smoke publish-local pubishLocal check clean jenkins-build coverage scaladoc test checkstyle compile \
mill.build mill.test mill.publishLocal mill.build.all mill.test.all mill.publishLocal.all mill-tools

default: publishLocal

Expand All @@ -38,8 +42,6 @@ publish-local publishLocal:
test:
$(SBT) $(SBT_FLAGS) test

check: test $(test_outs)

checkstyle:
$(SBT) $(SBT_FLAGS) scalastyle test:scalastyle

Expand Down Expand Up @@ -73,20 +75,34 @@ jenkins-build: clean
$(SBT) $(SBT_FLAGS) scalastyle coverage test
$(SBT) $(SBT_FLAGS) coverageReport

$(targetDir)/%.fir: $(test_src_dir)/%.scala
$(SBT) $(SBT_FLAGS) "test:runMain ChiselTests.MiniChisel $(notdir $(basename $<)) $(CHISEL_FLAGS)"
# Compile and package jar
mill.build: mill-tools
$(MILL) chisel3.jar

# Compile and test
mill.test: mill-tools
$(MILL) chisel3.test

# Build and publish jar
mill.publishLocal: mill-tools
$(MILL) chisel3.publishLocal

$(targetDir)/%.flo: $(targetDir)/%.fir
$(CHISEL_BIN)/fir2flo.sh $(targetDir)/$*
# Compile and package all jar
mill.build.all: mill-tools
$(MILL) chisel3[_].jar

$(targetDir)/%: $(targetDir)/%.flo $(targetDir)/emulator.h $(targetDir)/emulator_mod.h $(targetDir)/emulator_api.h
(cd $(targetDir); $(CHISEL_BIN)/flo2app.sh $*)
# Compile and test
mill.test.all: mill-tools
$(MILL) chisel3[_].test

$(targetDir)/%.h: $(c_resources_dir)/%.h
cp $< $@
# Build and publish jar
mill.publishLocal.all: mill-tools
$(MILL) chisel3[_].publishLocal

$(targetDir)/%.out: $(targetDir)/%
$(SBT) $(SBT_FLAGS) "test:runMain ChiselTests.MiniChisel $(notdir $(basename $<)) $(CHISEL_FLAGS) --test --targetDir $(targetDir)"
# Remove all generated code.
# Until "mill clean" makes it into a release.
mill.clean:
$(RM) -rf out

# The "last-resort" rule.
# We assume the target is something like "+clean".
Expand Down
12 changes: 12 additions & 0 deletions build.sbt
Expand Up @@ -125,6 +125,18 @@ lazy val coreMacros = (project in file("coreMacros")).
lazy val chiselFrontend = (project in file("chiselFrontend")).
settings(commonSettings: _*).
settings(publishArtifact := false).
settings(
scalacOptions := scalacOptions.value ++ Seq(
"-deprecation",
"-explaintypes",
"-feature",
"-language:reflectiveCalls",
"-unchecked",
"-Xcheckinit",
"-Xlint:infer-any"
// "-Xlint:missing-interpolator"
)
).
dependsOn(coreMacros)

// This will always be the root project, even if we are a sub-project.
Expand Down

0 comments on commit 1894319

Please sign in to comment.