Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/liboslcomp/oslcomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/liboslcomp/oslcomp_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/liboslexec/osolex.l
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/oslc/oslcmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
;
}
Expand Down Expand Up @@ -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]);
Expand Down