Boost logo

Boost-Commit :

From: oryol_at_[hidden]
Date: 2008-07-24 23:27:23


Author: jeremypack
Date: 2008-07-24 23:27:23 EDT (Thu, 24 Jul 2008)
New Revision: 47781
URL: http://svn.boost.org/trac/boost/changeset/47781

Log:
Runtime compilation first draft - very quick and dirty, but it works.

Added:
   sandbox/libs/extension/examples/runtime_compilation/
   sandbox/libs/extension/examples/runtime_compilation/Jamfile.v2 (contents, props changed)
   sandbox/libs/extension/examples/runtime_compilation/bjam/
   sandbox/libs/extension/examples/runtime_compilation/bjam/Jamfile.v2 (contents, props changed)
   sandbox/libs/extension/examples/runtime_compilation/bjam/bjam_compilation.cpp (contents, props changed)
   sandbox/libs/extension/examples/runtime_compilation/compilation.hpp (contents, props changed)
Text files modified:
   sandbox/libs/extension/examples/Jamfile.v2 | 21 +++------------------
   1 files changed, 3 insertions(+), 18 deletions(-)

Modified: sandbox/libs/extension/examples/Jamfile.v2
==============================================================================
--- sandbox/libs/extension/examples/Jamfile.v2 (original)
+++ sandbox/libs/extension/examples/Jamfile.v2 2008-07-24 23:27:23 EDT (Thu, 24 Jul 2008)
@@ -25,9 +25,6 @@
     :
     ;
 
-exe HelloWorld : main.cpp ;
-lib HelloWorldLib : hello_world.cpp : <link>shared ;
-
 exe HelloWorldVersions : versioning/main_versions.cpp ;
 lib HelloWorldLibv2 : versioning/hello_world_versions.cpp : <link>shared ;
 lib SaluteLib : versioning/salute.cpp : <link>shared ;
@@ -56,29 +53,17 @@
 ;
 exe MultipleInheritance : multiple_inheritance/main_mi.cpp Computer Vehicle ;
 
-#lib RegistryLibrary : registry/registry_library.cpp : <link>shared ;
-#exe RegistryExample : registry/registry_example.cpp ;
-
-stage ../stage :
- HelloWorld HelloWorldLib
- MultilanguageWord MultilanguageHelloWorld
- Vehicle Boat FlyingCar Car Plane
- CarOfTheFuture MultipleInheritance
- # RegistryLibrary
- Computer
- # RegistryExample
- ;
+lib tutorial_1 : tutorial_1/hello_world.cpp : <link>shared ;
+exe tutorial_1_bin : tutorial_1/main.cpp ;
 
 install ../test/ :
- HelloWorld HelloWorldLib
+ tutorial_1 tutorial_1_bin
   Parameters ParametersLib
   MultilanguageWord MultilanguageHelloWorld
   IM IMPlugins
   Vehicle Boat FlyingCar Car Plane
   CarOfTheFuture MultipleInheritance
- # RegistryLibrary
   Computer
- # RegistryExample
   :
   <install-dependencies>on
   <install-type>EXE

Added: sandbox/libs/extension/examples/runtime_compilation/Jamfile.v2
==============================================================================

Added: sandbox/libs/extension/examples/runtime_compilation/bjam/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/runtime_compilation/bjam/Jamfile.v2 2008-07-24 23:27:23 EDT (Thu, 24 Jul 2008)
@@ -0,0 +1,31 @@
+# Boost.Extension - examples Jamfile
+#
+# Copyright 2007 Jeremy Pack
+# Distributed under the Boost Software License, Version 1.0. (See
+# accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+#
+# See http://www.boost.org/ for latest version.
+#
+
+import type : change-generated-target-suffix ;
+import type : change-generated-target-prefix ;
+type.change-generated-target-suffix SHARED_LIB : : extension ;
+type.change-generated-target-prefix SHARED_LIB : : lib ;
+import os ;
+
+local BOOST_ROOT = [ os.environ BOOST_ROOT ] ;
+project
+ : requirements
+
+ <library>$(BOOST_ROOT)/libs/filesystem/build//boost_filesystem/<link>static
+ <library>$(BOOST_ROOT)/libs/system/build//boost_system/<link>static
+ <include>../../../
+ <include>$(BOOST_ROOT)
+ <toolset>gcc:<find-shared-library>dl
+ <toolset>gcc:<linkflags>"-Wl,-rpath,'$ORIGIN'"
+ <toolset>darwin:<define>DYLD_LIBRARY_PATH=./
+ :
+ ;
+
+exe bjam_compilation_bin : bjam_compilation.cpp ;

Added: sandbox/libs/extension/examples/runtime_compilation/bjam/bjam_compilation.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/runtime_compilation/bjam/bjam_compilation.cpp 2008-07-24 23:27:23 EDT (Thu, 24 Jul 2008)
@@ -0,0 +1,134 @@
+/*
+ * Boost.Extension / bjam_compilation example source:
+ * main file for bjam compilation
+ *
+ * (C) Copyright Jeremy Pack 2008
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+
+#include "../compilation.hpp"
+
+#include <iostream>
+
+#include <boost/bind.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/filesystem/fstream.hpp>
+#include <boost/extension/shared_library.hpp>
+
+namespace boost {
+namespace extensions {
+namespace impl {
+const char* boost_build_contents =
+"local rule if-has-file ( file + : dir * ) \n"
+"{ \n"
+" local result ;\n"
+" if $(dir) { \n"
+" result = [ GLOB $(dir) : $(file) ] ;\n"
+" } return $(result[1]:P) ; }\n"
+" local boost-src = [ if-has-file configure : [ MATCH --boost=(.*)\n"
+" : $(ARGV) ] $(BOOST) $(.boost-build-file:D)/../boost ] ;\n"
+" local boost-build-src = [ if-has-file bootstrap.jam :\n"
+" [ MATCH --boost-build=(.*) : $(ARGV) ] $(BOOST_BUILD_PATH) $(BOOST_BUILD)\n"
+" $(boost-src)/tools/build/v2 ] ; BOOST ?= $(boost-src) ;\n"
+" BOOST_ROOT ?= $(boost-src) ; boost-build $(boost-build-src) ;\n";
+
+const char* jamfile_preamble =
+"import type : change-generated-target-suffix ; \n"
+"import type : change-generated-target-prefix ; \n"
+"type.change-generated-target-suffix SHARED_LIB : : extension ; \n"
+"type.change-generated-target-prefix SHARED_LIB : : lib ; \n"
+"import os ; \n"
+"local BOOST_ROOT = [ os.environ BOOST_ROOT ] ; \n"
+"local BOOST_SANDBOX_ROOT = [ os.environ BOOST_SANDBOX_ROOT ] ; \n"
+"project : requirements <include>$(BOOST_SANDBOX_ROOT) <include>$(BOOST_ROOT) \n"
+"<toolset>gcc:<find-shared-library>dl \n"
+"<toolset>gcc:<linkflags>\"-Wl,-rpath,'$ORIGIN'\" \n"
+"<toolset>darwin:<define>DYLD_LIBRARY_PATH=./ : ; \n";
+
+void PrintHeader(std::ostream& out, const std::string& header_location) {
+ out << "#include " << header_location << std::endl;
+}
+
+} // namespace impl
+class bjam_compilation : public compilation {
+public:
+ virtual bool run(const std::string& library_name,
+ const std::string& external_function_contents,
+ type_map& types,
+ const std::string& earlier_file_contents = "") const {
+ using namespace boost::filesystem;
+ path initial_path(current_path());
+ if (!compilation_directory_.empty()) {
+ std::system(("cd " + compilation_directory_).c_str());
+ }
+ path compilation_path(current_path());
+ ofstream o(compilation_path / "boost-build.jam");
+ o << "# Start\n\n"
+ << std::string(impl::boost_build_contents) << std::endl;
+ ofstream p(compilation_path / "Jamfile.v2");
+ p << "# Start\n\n"
+ << std::string(impl::jamfile_preamble)
+ << " lib " << library_name << " : " << library_name
+ << ".cpp ;\ninstall . : " << library_name << " ;"
+ << std::endl;
+ p.close();
+ ofstream r(compilation_path / "Jamroot");
+ r << "# empty" << std::endl;
+ r.close();
+ ofstream cpp_file(compilation_path / (library_name + ".cpp"));
+ cpp_file << "// Boost.Extension generated file" << std::endl;
+ std::for_each(headers_.begin(), headers_.end(),
+ boost::bind(impl::PrintHeader, boost::ref(cpp_file), _1));
+ cpp_file <<
+ "\n\nextern \"C\" "
+ "void BOOST_EXTENSION_EXPORT_DECL\n"
+ "boost_extension_compiled_function("
+ "boost::extensions::type_map& types) {\n"
+ << external_function_contents << "\n}" << std::endl;
+ cpp_file.close();
+ int result = 0;
+ if ((result = std::system("~/bin/bjam --toolset=darwin --d+2")) != 0) {
+ std::cerr << "Compilation failed." << std::endl;
+ return false;
+ }
+ std::cout << "Compilation result: " << result << std::endl;
+ shared_library library("lib" + library_name + ".extension");
+ if (!library.open()) {
+ std::cerr << "Library not found." << std::endl;
+ return false;
+ }
+ typedef void (*compiled_func)(type_map&);
+ compiled_func func =
+ library.get<void, type_map&>("boost_extension_compiled_function");
+ if (!func) {
+ std::cerr << "Function not found." << std::endl;
+ return false;
+ }
+ func(types);
+ std::system(("cd " + compilation_path.string()).c_str());
+ return true;
+ }
+};
+} // namespace extensions
+} // namespace boost
+
+
+int main(int argc, char* argv[]) {
+ using namespace boost::extensions;
+ bjam_compilation c;
+ c.add_header("<iostream>");
+ type_map types;
+ if (c.run("test_library",
+ "std::cout << \"Inside the shared library: Yay!\" << std::endl;",
+ types)) {
+ std::cout << "Success!" << std::endl;
+ } else {
+ std::cout << "Failure!" << std::endl;
+ }
+ return 0;
+}

Added: sandbox/libs/extension/examples/runtime_compilation/compilation.hpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/examples/runtime_compilation/compilation.hpp 2008-07-24 23:27:23 EDT (Thu, 24 Jul 2008)
@@ -0,0 +1,58 @@
+/*
+ * Boost.Extension / compilation example header:
+ * main header for compilations
+ *
+ * (C) Copyright Jeremy Pack 2008
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ *
+ * See http://www.boost.org/ for latest version.
+ */
+
+#ifndef BOOST_EXTENSION_COMPILATION_HPP
+#define BOOST_EXTENSION_COMPILATION_HPP
+
+#include <vector>
+#include <string>
+
+#include <boost/extension/type_map.hpp>
+
+namespace boost {
+namespace extensions {
+class compilation {
+public:
+ virtual bool run(const std::string& library_name,
+ const std::string& external_function_contents,
+ type_map& types,
+ const std::string& earlier_file_contents = "") const = 0;
+ void add_header(const std::string& location) {
+ headers_.push_back(location);
+ }
+ void vector_compilation_directory(const std::string& directory) {
+ compilation_directory_ = directory;
+ }
+ void add_include_path(const std::string& location) {
+ headers_.push_back(location);
+ }
+ void add_source(const std::string& location) {
+ headers_.push_back(location);
+ }
+ void add_static_library(const std::string& name) {
+ headers_.push_back(name);
+ }
+ compilation() {
+ headers_.push_back("<boost/extension/extension.hpp>");
+ headers_.push_back("<boost/extension/type_map.hpp>");
+ }
+ virtual ~compilation() {}
+protected:
+ std::vector<std::string> headers_;
+ std::vector<std::string> include_path_;
+ std::vector<std::string> sources_;
+ std::vector<std::string> libraries_;
+ std::string compilation_directory_;
+};
+} // namespace extensions
+} // namespace boost
+#endif // BOOST_EXTENSION_COMPILATION_HPP


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk