Skip to content

Commit

Permalink
Component bundled Jar files now update classpath.
Browse files Browse the repository at this point in the history
Now when bundling Java .jar files in a compoent the mktools will detect
this and update the executable's classpath to include those bundled
.jar files.

Resolves: LE-4657
Change-Id: Ie134fe81c8777ea6ae084b57da4f351b556b57bf
  • Loading branch information
Chloe Strainge authored and jbacksierrawireless committed Apr 19, 2018
1 parent 3eb78c5 commit d3fd4ed
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 22 deletions.
3 changes: 1 addition & 2 deletions apps/sample/javaHelloWorld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ TARGETS := $(MAKECMDGOALS)
all: $(TARGETS)

$(TARGETS):
mkapp -v -t $@ \
javaHelloWorld.adef
mkapp -v -t $@ -s $(LEGATO_ROOT)/components javaHelloWorld.adef

clean:
rm -rf _build_* *.update
Expand Down
34 changes: 29 additions & 5 deletions framework/doc/defFilesCdef.dox
Original file line number Diff line number Diff line change
Expand Up @@ -789,9 +789,8 @@ loops and report any error.


@section defFilesCdef_requiresJavaPackage javaPackage

You create a @c javaPackage: section in the @ .cdef listing Java packages to build. It should look
something like this:
When creating a Java based component, you can create a @c javaPackage: section in the @ .cdef
listing Java packages to build. It should look something like this:

@code
javaPackage:
Expand All @@ -800,8 +799,8 @@ javaPackage:
}
@endcode


The build tools will look for Java to code under @c COMPONENT_DIR/src/io/legato/samples/ *.java
Where you supply the name of the package where your java code will be found. The build tools will
look for Java to code under @c COMPONENT_DIR/src/io/legato/samples/ *.java

It won't recurse automatically into subdirectories; if you want subdirectories, they also must
added to the JavaPackage section.
Expand Down Expand Up @@ -831,6 +830,31 @@ A hello world Java app folder structure should look something like this:
+-- jHello.adef
@endcode

It is also possible to bundle pre-built Jar files with your component. Simply add the Jar files to
your bundles section of the same @c .cdef file containing your Java source. Note that the extension
of the file does have to be @c .jar in order to be detected by the build tools.

For example in your @c Component.cdef:

@code
javaPackage:
{
com.yourcompany.yourproject
}

bundles:
{
file:
{
path/to/my/lib/MyHandyLib.jar /lib/
}
}
@endcode

Will bundle the lib @c MyHandyLib.jar in the application's @c lib directory. In addition to this,
the Jar file will also be automatically be added in your classpath. Allowing your code to make use
of the code within that Java library.

@section defFilesCdef_sources sources

Contains a list of source code files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,7 @@ void ComponentBuildScriptGenerator_t::GenerateJavaBuildCommand
const std::string& outputJar,
const std::string& classDestPath,
const std::list<std::string>& sources,
const std::list<std::string>& classPath,
const std::list<std::string>& dependencies
const std::list<std::string>& jarClassPath
)
//--------------------------------------------------------------------------------------------------
{
Expand All @@ -656,17 +655,17 @@ void ComponentBuildScriptGenerator_t::GenerateJavaBuildCommand

script << " $\n |";

for (auto& dep : dependencies)
for (auto& dep : jarClassPath)
{
script << " " << dep;
}

script << "\n"
" classPath = ";

for (auto iter = classPath.begin(); iter != classPath.end(); ++iter)
for (auto iter = jarClassPath.begin(); iter != jarClassPath.end(); ++iter)
{
if (iter != classPath.begin())
if (iter != jarClassPath.begin())
{
script << ":";
}
Expand All @@ -680,9 +679,9 @@ void ComponentBuildScriptGenerator_t::GenerateJavaBuildCommand
" : MakeJar " << path::Combine(classDestPath, "build.stamp") << "\n"
" classPath = ";

for (auto iter = classPath.begin(); iter != classPath.end(); ++iter)
for (auto iter = jarClassPath.begin(); iter != jarClassPath.end(); ++iter)
{
if (iter != classPath.begin())
if (iter != jarClassPath.begin())
{
script << ":";
}
Expand Down Expand Up @@ -764,11 +763,14 @@ void ComponentBuildScriptGenerator_t::GenerateBuildStatements
"build/$target/framework/lib/legato.jar");
auto classDestPath = "$builddir/" + componentPtr->workingDir + "/obj";

// Append to the class path based on the component's bundled .jar files.
std::list<std::string> classPath = { legatoJarPath };
componentPtr->GetBundledFilesOfType(model::BundleAccess_t::Source, ".jar", classPath);

GenerateJavaBuildCommand(componentPtr->getTargetInfo<target::LinuxComponentInfo_t>()->lib,
classDestPath,
sourceList,
{ legatoJarPath },
{ legatoJarPath });
classPath);
}
else if (componentPtr->HasExternalBuild())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ class ComponentBuildScriptGenerator_t : protected RequireBaseGenerator_t
virtual void GenerateJavaBuildCommand(const std::string& outputJar,
const std::string& classDestPath,
const std::list<std::string>& sources,
const std::list<std::string>& classPath,
const std::list<std::string>& dependencies);
const std::list<std::string>& jarClassPath);
virtual void GenerateRunPathLdFlags(void);

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,16 @@ void ExeBuildScriptGenerator_t::GenerateBuildStatements
{
classPath.push_back(componentPtr->getTargetInfo<target::LinuxComponentInfo_t>()
->lib);
dependencies.push_back(componentPtr->getTargetInfo<target::LinuxComponentInfo_t>()
->lib);
componentPtr->GetBundledFilesOfType(model::BundleAccess_t::Source,
".jar",
classPath);
}
}

componentGeneratorPtr->GenerateJavaBuildCommand(path::Combine("$builddir/", exePtr->path),
classDestPath,
{ mainObjectFile.sourceFilePath },
classPath,
dependencies);
classPath);
}
else if (exePtr->hasPythonCode)
{
Expand Down
41 changes: 41 additions & 0 deletions framework/tools/mkTools/conceptualModel/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,47 @@ Component_t* Component_t::CreateComponent
}


//--------------------------------------------------------------------------------------------------
/**
* Build a list of bundled files that are of the same type.
**/
//--------------------------------------------------------------------------------------------------
void Component_t::GetBundledFilesOfType
(
BundleAccess_t access, ///< Are we searching the source or dest paths?
const std::string& fileType, ///< What kind of file are we looking for?
std::list<std::string>& fileList ///< Add the found files to this list.
)
//--------------------------------------------------------------------------------------------------
{
for (const auto& bundledDir : bundledDirs)
{
const auto& dirPath = bundledDir->GetBundledPath(access);
auto bundledFiles = file::ListFiles(dirPath);

for (const auto& file : bundledFiles)
{
if (path::GetFileNameExtension(file) == fileType)
{
fileList.push_back(file);
}
}
}

for (const auto& bundledFile : bundledFiles)
{
const auto& filePath = bundledFile->GetBundledPath(access);
auto extension = path::GetFileNameExtension(filePath);

if (extension == fileType)
{
fileList.push_back(filePath);
}
}
}



//--------------------------------------------------------------------------------------------------
/**
* Throw error message about incompatible source or build methods.
Expand Down
3 changes: 3 additions & 0 deletions framework/tools/mkTools/conceptualModel/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ struct Component_t : public HasTargetInfo_t
// @throw model::Exception_t if already exists.
static Component_t* CreateComponent(const parseTree::CdefFile_t* filePtr);

void GetBundledFilesOfType(BundleAccess_t access, const std::string& fileType,
std::list<std::string>& fileList);

// Does the component have C code?
bool HasCCode() const
{
Expand Down
28 changes: 28 additions & 0 deletions framework/tools/mkTools/conceptualModel/fileSystemObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
#define LEGATO_MKTOOLS_MODEL_FILE_SYSTEM_OBJECT_H_INCLUDE_GUARD


//--------------------------------------------------------------------------------------------------
/**
* Enumeration used by GetBundledPath to enable generically accesing the source and destination of
* the FileSystem object.
*/
//--------------------------------------------------------------------------------------------------
enum class BundleAccess_t
{
Source,
Dest
};


//--------------------------------------------------------------------------------------------------
/**
* Represents a file system object, such as a file or directory.
Expand Down Expand Up @@ -40,6 +53,21 @@ struct FileSystemObject_t
std::string destPath; ///< Path to where the object will be put on target.
Permissions_t permissions; ///< Read, write, and/or execute permissions on the object.

// Return either the source or dest path based on the accessFlag.
inline const std::string& GetBundledPath(BundleAccess_t accessFlag) const
{
switch (accessFlag)
{
case BundleAccess_t::Source:
return srcPath;

case BundleAccess_t::Dest:
return destPath;
}

throw mk::Exception_t(LE_I18N("Unknown bundled file access type."));
}

/// Two file sytem objects refer to the same file if the destination paths are the same.
bool operator==(const FileSystemObject_t& a) const
{
Expand Down
8 changes: 8 additions & 0 deletions framework/tools/mkTools/configGenerator/configGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,14 @@ static std::string GenerateClassPath

if (componentPtr->HasJavaCode())
{
std::list<std::string> bundledJars;
componentPtr->GetBundledFilesOfType(model::BundleAccess_t::Dest, ".jar", bundledJars);

for (const auto& jarFile : bundledJars)
{
classPath += ":" + jarFile;
}

classPath += ":lib/" +
path::GetLastNode(componentPtr->getTargetInfo<target::LinuxComponentInfo_t>()->lib);
}
Expand Down

0 comments on commit d3fd4ed

Please sign in to comment.