ROSE Compiler Framework/How to set up the makefile for a translator

In this How-to, you will create a makefile to compile and test your own custom ROSE translator.

You may want to first look at "How-to install ROSE": ROSE Compiler Framework/Installation.

Environment variables
You must have the proper environment variable set so you translator can find the librose.so during execution. export LD_LIBRARY_PATH=${ROSE_INSTALL}/lib:${BOOST_INSTALL}/lib:$LD_LIBRARY_PATH

Translator Code
Here is a simplest ROSE translator. // ROSE translator example: identity translator. // // No AST manipulations, just a simple translation: // //   input_code > ROSE AST > output_code


 * 1) include 

int main (int argc, char** argv) {   // Build the AST used by ROSE SgProject* project = frontend(argc, argv);

// Run internal consistency tests on AST AstTests::runAllTests(project);

// Insert your own manipulations of the AST here...

// Generate source code from AST and invoke your // desired backend compiler return backend(project); }

Example 1
If you have a project that's separate from ROSE (i.e., you compile it with an *installed* version of ROSE) it's up to you how to do things.

If the project depends only on ROSE and ROSE's dependencies then you can use the Makefile described at the end of the ROSE installation instructions http://rosecompiler.org/ROSE_HTML_Reference/installation.html


 * 1) Sample makefile for programs that use the ROSE library.
 * 2) ROSE has a number of configuration details that must be present when
 * 3) compiling and linking a user program with ROSE, and some of these
 * 4) details are difficult to get right.  The most foolproof way to get
 * 5) these details into your own makefile is to use the "rose-config"
 * 6) tool.
 * 7) This makefile assumes:
 * 8)   1. The ROSE library has been properly installed (refer to the
 * 9)      documentation for configuring, building, and installing ROSE).
 * 10)   2. The top of the installation directory is $(ROSE_INSTALLED). This
 * 11)      is the same directory you specified for the "--prefix" argument
 * 12)      of the "configure" script, or the "CMAKE_INSTALL_PREFIX" if using
 * 13)      cmake. E.g., "/usr/local".
 * 14) The "rose-config" tool currently only works for ROSE configured with
 * 15) GNU auto tools (e.g., you ran "configure" when you built and
 * 16) installed ROSE). The "cmake" configuration is not currently
 * 17) supported by "rose-config" [September 2015].
 * 1) The "rose-config" tool currently only works for ROSE configured with
 * 2) GNU auto tools (e.g., you ran "configure" when you built and
 * 3) installed ROSE). The "cmake" configuration is not currently
 * 4) supported by "rose-config" [September 2015].
 * 1) supported by "rose-config" [September 2015].

CXX     = $(shell $(ROSE_INSTALLED)/bin/rose-config cxx) CPPFLAGS = $(shell $(ROSE_INSTALLED)/bin/rose-config cppflags) CXXFLAGS = $(shell $(ROSE_INSTALLED)/bin/rose-config cxxflags) LDFLAGS = $(shell $(ROSE_INSTALLED)/bin/rose-config ldflags) LIBDIRS = $(shell $(ROSE_INSTALLED)/bin/rose-config libdirs)
 * 1) Standard C++ compiler stuff (see rose-config --help)

MOSTLYCLEANFILES =


 * 1) Assuming your source code is "demo.C" to build an executable named "demo".
 * 1) Assuming your source code is "demo.C" to build an executable named "demo".

all: demo

demo.o: demo.C  $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $^

demo: demo.o  $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) @echo "Remember to set:" @echo " LD_LIBRARY_PATH=$(LIBDIRS):$$LD_LIBRARY_PATH"

MOSTLYCLEANFILES += demo demo.o


 * 1) Standard boilerplate
 * 1) Standard boilerplate

.PHONY: clean clean: rm -f $(MOSTLYCLEANFILES)

Complete examples
There are project examples demonstrating different ways of building your projects using ROSE's headers/libraries.

They are available at: https://github.com/chunhualiao/rose-project-templates

A few templates for independent projects using ROSE. By independent, we mean the projects are located outside of ROSE's source tree.
 * template-project-v1 : using Makefile to build the project
 * template-project-v2 : using Makefile to build and run a ROSE plugin