Skip to content

Commit

Permalink
Allow to set binary/text modfiles in cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
certik committed Jun 18, 2021
1 parent e5217e8 commit 5a8c826
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ set(WITH_LFORTRAN_ASSERT ${WITH_LFORTRAN_ASSERT_DEFAULT}
# LFORTRAN_STATIC_BIN
set(LFORTRAN_STATIC_BIN no CACHE BOOL "Build LFortran as a static binary")

# WITH_LFORTRAN_BINARY_MODFILES
set(WITH_LFORTRAN_BINARY_MODFILES YES
CACHE BOOL "Use binary modfiles")

#ZLIB
find_package(ZLIB REQUIRED)

Expand Down Expand Up @@ -225,6 +229,7 @@ message("WITH_LLVM: ${WITH_LLVM}")
message("WITH_XEUS: ${WITH_XEUS}")
message("WITH_JSON: ${WITH_JSON}")
message("WITH_FMT: ${WITH_FMT}")
message("WITH_LFORTRAN_BINARY_MODFILES: ${WITH_LFORTRAN_BINARY_MODFILES}")


add_subdirectory(src)
3 changes: 3 additions & 0 deletions src/lfortran/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
/* Define if XEUS is enabled */
#cmakedefine HAVE_LFORTRAN_XEUS

/* Define if we should use binary modfiles */
#cmakedefine WITH_LFORTRAN_BINARY_MODFILES

#endif // LFORTRAN_CONFIG_H
9 changes: 9 additions & 0 deletions src/lfortran/modfile.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string>

#include <lfortran/config.h>
#include <lfortran/asr_utils.h>
#include <lfortran/asr_verify.h>
#include <lfortran/modfile.h>
Expand Down Expand Up @@ -29,7 +30,11 @@ std::string save_modfile(const ASR::TranslationUnit_t &m) {
LFORTRAN_ASSERT(ASR::is_a<ASR::Module_t>(*a.second));
if ((bool&)a) { } // Suppress unused warning in Release mode
}
#ifdef WITH_LFORTRAN_BINARY_MODFILES
BinaryWriter b;
#else
TextWriter b;
#endif
// Header
b.write_string(lfortran_modfile_type_string);
b.write_string(LFORTRAN_VERSION);
Expand All @@ -54,7 +59,11 @@ std::string save_modfile(const ASR::TranslationUnit_t &m) {

ASR::TranslationUnit_t* load_modfile(Allocator &al, const std::string &s,
bool load_symtab_id, SymbolTable &symtab) {
#ifdef WITH_LFORTRAN_BINARY_MODFILES
BinaryReader b(s);
#else
TextReader b(s);
#endif
std::string file_type = b.read_string();
if (file_type != lfortran_modfile_type_string) {
throw LFortranException("LFortran Modfile format not recognized");
Expand Down
21 changes: 9 additions & 12 deletions src/lfortran/serialization.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#include <string>

#include <lfortran/config.h>
#include <lfortran/serialization.h>
#include <lfortran/parser/parser.h>
#include <lfortran/parser/parser.tab.hh>
#include <lfortran/asr_utils.h>
#include <lfortran/asr_verify.h>
#include <lfortran/bwriter.h>

// Comment this out to enable human readable text Writer/Reader
// Useful for debugging.
#define SER_BINARY

namespace LFortran {


class ASTSerializationVisitor :
#ifdef SER_BINARY
#ifdef WITH_LFORTRAN_BINARY_MODFILES
public BinaryWriter,
#else
public TextWriter,
Expand Down Expand Up @@ -44,7 +41,7 @@ std::string serialize(const AST::TranslationUnit_t &unit) {
}

class ASTDeserializationVisitor :
#ifdef SER_BINARY
#ifdef WITH_LFORTRAN_BINARY_MODFILES
public BinaryReader,
#else
public TextReader,
Expand All @@ -53,7 +50,7 @@ class ASTDeserializationVisitor :
{
public:
ASTDeserializationVisitor(Allocator &al, const std::string &s) :
#ifdef SER_BINARY
#ifdef WITH_LFORTRAN_BINARY_MODFILES
BinaryReader(s),
#else
TextReader(s),
Expand Down Expand Up @@ -82,7 +79,7 @@ AST::ast_t* deserialize_ast(Allocator &al, const std::string &s) {
// ----------------------------------------------------------------

class ASRSerializationVisitor :
#ifdef SER_BINARY
#ifdef WITH_LFORTRAN_BINARY_MODFILES
public BinaryWriter,
#else
public TextWriter,
Expand Down Expand Up @@ -117,17 +114,17 @@ std::string serialize(const ASR::TranslationUnit_t &unit) {
}

class ASRDeserializationVisitor :
#ifdef SER_BINARY
public BinaryReader,
#ifdef WITH_LFORTRAN_BINARY_MODFILES
public BinaryReader,
#else
public TextReader,
public TextReader,
#endif
public ASR::DeserializationBaseVisitor<ASRDeserializationVisitor>
{
public:
ASRDeserializationVisitor(Allocator &al, const std::string &s,
bool load_symtab_id) :
#ifdef SER_BINARY
#ifdef WITH_LFORTRAN_BINARY_MODFILES
BinaryReader(s),
#else
TextReader(s),
Expand Down

0 comments on commit 5a8c826

Please sign in to comment.