Skip to content
Permalink
Browse files

oslc --embed-source : embed preprocessed source at end of oso file (#…

…1081)

This can be useful in debugging situations, since the oso file would
completly contain a readable (though preprocessed) and recompilable
version of the source.

In the oso file, this is signified by the hint `%preprocessed_source`,
and following that the remainder of the file is assumed to be source
code or comments and will be ignored by the oso parser.
  • Loading branch information
lgritz committed Dec 2, 2019
1 parent 1e68690 commit 3aba693fb5223d598fdd2ae4a03a0acd5a3fe589
Showing with 25 additions and 5 deletions.
  1. +14 −3 src/liboslcomp/oslcomp.cpp
  2. +3 −1 src/liboslcomp/oslcomp_pvt.h
  3. +4 −0 src/liboslexec/osolex.l
  4. +4 −1 src/oslc/oslcmain.cpp
@@ -418,6 +418,8 @@ OSLCompilerImpl::read_compile_options (const std::vector<std::string> &options,
m_optimizelevel = 2;
} else if (options[i] == "-Werror") {
m_err_on_warning = true;
} else if (options[i] == "-embed-source" || options[i] == "--embed-source") {
m_embed_source = true;
} else if (options[i].c_str()[0] == '-' && options[i].size() > 2) {
// options meant for the preprocessor
if (options[i].c_str()[1] == 'D' || options[i].c_str()[1] == 'U')
@@ -585,7 +587,9 @@ OSLCompilerImpl::compile (string_view filename,
ASSERT (m_osofile == NULL);
m_osofile = &oso_output;

write_oso_file (m_output_filename, OIIO::Strutil::join(options," "));
write_oso_file (m_output_filename,
OIIO::Strutil::join(options," "),
preprocess_result);
ASSERT (m_osofile == NULL);
}

@@ -666,7 +670,9 @@ OSLCompilerImpl::compile_buffer (string_view sourcecode,
ASSERT (m_osofile == NULL);
m_osofile = &oso_output;

write_oso_file (m_output_filename, OIIO::Strutil::join(options," "));
write_oso_file (m_output_filename,
OIIO::Strutil::join(options," "),
preprocess_result);
osobuffer = oso_output.str();
ASSERT (m_osofile == NULL);
}
@@ -897,7 +903,8 @@ OSLCompilerImpl::write_oso_symbol (const Symbol *sym)

void
OSLCompilerImpl::write_oso_file (const std::string &outfilename,
string_view options)
string_view options,
string_view preprocessed_source)
{
ASSERT (m_osofile != NULL && m_osofile->good());
oso ("OpenShadingLanguage %d.%02d\n",
@@ -1028,6 +1035,10 @@ OSLCompilerImpl::write_oso_file (const std::string &outfilename,
oso ("code %s\n", main_method_name().c_str());

oso ("\tend\n");

if (m_embed_source)
oso ("%%preprocessed_source\n%s\n", preprocessed_source);

m_osofile = NULL;
}

@@ -397,7 +397,8 @@ class OSLCompilerImpl {
void initialize_globals ();
void initialize_builtin_funcs ();
std::string default_output_filename ();
void write_oso_file (const std::string &outfilename, string_view options);
void write_oso_file (const std::string &outfilename, string_view options,
string_view preprocessed_source="");
void write_oso_const_value (const ConstantSymbol *sym) const;
void write_oso_symbol (const Symbol *sym);
void write_oso_metadata (const ASTNode *metanode) const;
@@ -478,6 +479,7 @@ class OSLCompilerImpl {
bool m_quiet; ///< Quiet mode
bool m_debug; ///< Debug mode
bool m_preprocess_only; ///< Preprocess only?
bool m_embed_source = false; ///< Embed preprocessed source in oso?
bool m_err_on_warning; ///< Treat warnings as errors?
int m_optimizelevel; ///< Optimization level
OpcodeVec m_ircode; ///< Generated IR code
@@ -230,6 +230,10 @@ using namespace OSL::pvt;
return STRING_LITERAL;
}
\%preprocessed_source\n(.*\n)* {
/* skip remainder of file */
}
{HINTPATTERN} {
ustring s (yytext);
yylval.s = s.c_str();
@@ -62,6 +62,7 @@ usage ()
"\t-d Debug mode\n"
"\t-E Only preprocess the input and output to stdout\n"
"\t-Werror Treat all warnings as errors\n"
"\t-embed-source Embed preprocessed source in the oso file\n"
"\t-buffer (debugging) Force compile from buffer\n"
;
}
@@ -147,7 +148,9 @@ main (int argc, const char *argv[])
! strcmp (argv[a], "-E") ||
! strcmp (argv[a], "-O") || ! strcmp (argv[a], "-O0") ||
! strcmp (argv[a], "-O1") || ! strcmp (argv[a], "-O2") ||
! strcmp (argv[a], "-Werror")
! strcmp (argv[a], "-Werror") ||
! strcmp (argv[a], "-embed-source") ||
! strcmp (argv[a], "--embed-source")
) {
// Valid command-line argument
args.emplace_back(argv[a]);

0 comments on commit 3aba693

Please sign in to comment.
You can’t perform that action at this time.