<commit_before>require "#{ENV['CORTO_BUILD']}/common"
require 'rake/clean'

PACKAGE_FWSLASH = PACKAGE.gsub("::", "/")

LIB_PUBLIC ||= [] + LIB
LIBPATH_PUBLIC ||= [] + LIBPATH
LINK_PUBLIC ||= ["."] + LINK
# keep track of the original LINK array as the buildsystem will append LINK with
# dependencies
LINK_NO_DEPS = LINK.clone
GENERATED_SOURCES ||= []
TARGET ||= PACKAGE_FWSLASH.split("/").last
DEFINE << "BUILDING_" + PACKAGE_FWSLASH.gsub("/", "_").upcase
NAME ||= PACKAGE_FWSLASH.split("/").last
ARTEFACT ||= TARGET
ARTEFACT_PREFIX ||= "lib"
ARTEFACT_EXT ||= "so"
INSTALL ||= "lib/corto"

# Preprocessor variables
PP_ATTR ||= []
if not defined? PP_OBJECTS then
  PP_SCOPES ||= [PACKAGE]
else
  if not PP_ATTR.include?("app=true") then
    STDERR.puts "\033[1;31mcorto: cannot use PP_OBJECTS for packages\033[0;49m"
    abort();
  end
  PP_SCOPES ||= []
end
PP_OBJECTS ||= []

if LANGUAGE != "cpp" and LANGUAGE != "c++" then
  PREFIX ||= TARGET
else
  PREFIX = "."
end

# Include corto package only when not building the core
if TARGET != "corto" and not defined? NOCORTO then
  USE_PACKAGE << "corto"
  if LANGUAGE == "cpp" or LANGUAGE == "c++" then
    USE_PACKAGE << "corto/cpp"
  elsif LANGUAGE == "c" or LANGUAGE == "c4cpp" then
    USE_PACKAGE << "corto/lang/c" << "corto/core/c"
  end
end

# Set working directory to where the rakefile is
CHDIR_SET = true
Dir.chdir(File.dirname(Rake.application.rakefile))

# Support for local packages (packages that are not installed to environment)
if LOCAL == true then
  TARGETPATH = "./.corto"
  TARGETDIR ||= TARGETPATH
  INCLUDE << "include"
else
  PACKAGEDIR = PACKAGE_FWSLASH
  TARGETPATH = PACKAGEDIR
  TARGETDIR ||= "#{CORTO_TARGET}/#{INSTALL}/#{CORTO_VERSION}/#{TARGETPATH}"
end

# Define a convenience macro in the package that points to the installed ETC directory
if not LOCAL then
  DEFINE << PACKAGE_FWSLASH.gsub("/", "_").upcase + "_ETC='\"#{CORTO_TARGET}/etc/corto/#{CORTO_VERSION}/#{TARGETPATH}\"'"
else
  DEFINE << PACKAGE_FWSLASH.gsub("/", "_").upcase + "_ETC='\"" + Dir.getwd + "/etc\"'"
end

# Rule for creating packages.txt
file ".corto/packages.txt" do
  verbose(VERBOSE)
  cmd "mkdir -p .corto"
  cmd "touch .corto/packages.txt"
end

# Code generation
if not defined? NOCORTO then
  verbose(VERBOSE)
  DEFFILES = Rake::FileList["#{NAME}.{cx,idl,xml}"]

  if DEFFILES.length != 0 then
    # If this is a corto package, let Corto manage which symbols should be
    # external
    CFLAGS << "-fvisibility=hidden"

    GENFILE = DEFFILES[0]

    if LANGUAGE == "c" or LANGUAGE == "c4cpp" then
      GENERATED_SOURCES <<
        ".corto/_api.#{EXT}" <<
        ".corto/_wrapper.#{EXT}" <<
        ".corto/_project.#{EXT}" <<
        ".corto/_load.#{EXT}"

      GENERATED_HEADERS ||= [] <<
        "include/_api.h" <<
        "include/_load.h" <<
        "include/_type.h" <<
        "include/_project.h"
    else
      GENERATED_SOURCES <<
        ".corto/_api.#{EXT}" <<
        ".corto/_project.#{EXT}" <<
        ".corto/_load.#{EXT}"

      GENERATED_HEADERS ||= [] <<
        "include/_load.h" <<
        "include/_type.h"
    end

    file "include/_type.h" => [GENFILE, ".corto/packages.txt"] do
      verbose(VERBOSE)
      preload = PP_PRELOAD.join(" ")
      cmd "mkdir -p .corto"
      cmd "touch .corto/_wrapper.#{EXT}"

      localStr = ""
      docStr = ""

      if LOCAL then
        localStr = "--attr local=true"
      else
        begin
          cmd "corto locate corto/gen/doc/doc --silent"
          # docStr = "-g doc/doc"
        rescue
        end
      end

      if defined? PREFIX then
        prefixStr = "--prefix #{PREFIX}"
      end

      command = "corto pp #{preload} #{GENFILE} --name #{PACKAGE} " +
                "#{PP_SCOPES.map{|s| "--scope " + s}.join(" ")} " +
                "#{PP_OBJECTS.map{|o| "--object " + o}.join(" ")} " +
                "--import #{USE_PACKAGE.join(",")} " +
                "#{PP_ATTR.map{|a| "--attr " + a}.join(" ")} " +
                "#{prefixStr} #{localStr} #{docStr} --lang #{LANGUAGE}"

      if ENV['silent'] != "true" then
        msg "preprocess #{C_NORMAL}#{GENFILE}"
      end
      begin
        cmd command
      rescue
        STDERR.puts "\033[1;31mcorto: command failed: #{command}\033[0;49m"
        if File.exists? "include/_type.h" then
          cmd "rm include/_type.h"
        end
        abort()
      end
    end
    task :default => ["include/_type.h"]
  else
    GENERATED_SOURCES <<
        ".corto/_project.#{EXT}"

    file ".corto/_project.#{EXT}" => [".corto/packages.txt"] do
      verbose(VERBOSE)
      preload = PP_PRELOAD.join(" ")
      cmd "mkdir -p .corto"

      localStr = ""

      if LOCAL then
        localStr = "--attr local=true"
      end
      if LANGUAGE == "c4cpp" then
        langStr = "--attr c4cpp=true"
      elsif LANGUAGE == "cpp" or LANGUAGE == "c++" then
        langStr = "--attr c4cpp=true --attr lang=cpp --attr hpp=include --attr cpp=src"
      end

      command =
        "corto pp #{preload} #{localStr} --name #{PACKAGE} " +
        "--attr h=include --attr c=src #{PP_ATTR.map{|a| "--attr " + a}.join(" ")} " +
        "--import #{USE_PACKAGE.join(",")} " +
        " -g c/project #{langStr}"

      if ENV['silent'] != "true" then
        msg "preprocess"
      end
      begin
        cmd command
      rescue
        STDERR.puts "\033[1;31mcorto: command failed: #{command}\033[0;49m"
        if File.exists? "include/#{NAME}.h" then
          cmd "rm include/#{NAME}.h"
        end
        abort()
      end
    end
    task :prebuild => [".corto/_project.#{EXT}"]
  end
end

# Document framework integration
task :doc do
  verbose(VERBOSE)<commit_msg>#341 temporarily add valgrind to build for debugging<commit_after>require "#{ENV['CORTO_BUILD']}/common"
require 'rake/clean'

PACKAGE_FWSLASH = PACKAGE.gsub("::", "/")

LIB_PUBLIC ||= [] + LIB
LIBPATH_PUBLIC ||= [] + LIBPATH
LINK_PUBLIC ||= ["."] + LINK
# keep track of the original LINK array as the buildsystem will append LINK with
# dependencies
LINK_NO_DEPS = LINK.clone
GENERATED_SOURCES ||= []
TARGET ||= PACKAGE_FWSLASH.split("/").last
DEFINE << "BUILDING_" + PACKAGE_FWSLASH.gsub("/", "_").upcase
NAME ||= PACKAGE_FWSLASH.split("/").last
ARTEFACT ||= TARGET
ARTEFACT_PREFIX ||= "lib"
ARTEFACT_EXT ||= "so"
INSTALL ||= "lib/corto"

# Preprocessor variables
PP_ATTR ||= []
if not defined? PP_OBJECTS then
  PP_SCOPES ||= [PACKAGE]
else
  if not PP_ATTR.include?("app=true") then
    STDERR.puts "\033[1;31mcorto: cannot use PP_OBJECTS for packages\033[0;49m"
    abort();
  end
  PP_SCOPES ||= []
end
PP_OBJECTS ||= []

if LANGUAGE != "cpp" and LANGUAGE != "c++" then
  PREFIX ||= TARGET
else
  PREFIX = "."
end

# Include corto package only when not building the core
if TARGET != "corto" and not defined? NOCORTO then
  USE_PACKAGE << "corto"
  if LANGUAGE == "cpp" or LANGUAGE == "c++" then
    USE_PACKAGE << "corto/cpp"
  elsif LANGUAGE == "c" or LANGUAGE == "c4cpp" then
    USE_PACKAGE << "corto/lang/c" << "corto/core/c"
  end
end

# Set working directory to where the rakefile is
CHDIR_SET = true
Dir.chdir(File.dirname(Rake.application.rakefile))

# Support for local packages (packages that are not installed to environment)
if LOCAL == true then
  TARGETPATH = "./.corto"
  TARGETDIR ||= TARGETPATH
  INCLUDE << "include"
else
  PACKAGEDIR = PACKAGE_FWSLASH
  TARGETPATH = PACKAGEDIR
  TARGETDIR ||= "#{CORTO_TARGET}/#{INSTALL}/#{CORTO_VERSION}/#{TARGETPATH}"
end

# Define a convenience macro in the package that points to the installed ETC directory
if not LOCAL then
  DEFINE << PACKAGE_FWSLASH.gsub("/", "_").upcase + "_ETC='\"#{CORTO_TARGET}/etc/corto/#{CORTO_VERSION}/#{TARGETPATH}\"'"
else
  DEFINE << PACKAGE_FWSLASH.gsub("/", "_").upcase + "_ETC='\"" + Dir.getwd + "/etc\"'"
end

# Rule for creating packages.txt
file ".corto/packages.txt" do
  verbose(VERBOSE)
  cmd "mkdir -p .corto"
  cmd "touch .corto/packages.txt"
end

# Code generation
if not defined? NOCORTO then
  verbose(VERBOSE)
  DEFFILES = Rake::FileList["#{NAME}.{cx,idl,xml}"]

  if DEFFILES.length != 0 then
    # If this is a corto package, let Corto manage which symbols should be
    # external
    CFLAGS << "-fvisibility=hidden"

    GENFILE = DEFFILES[0]

    if LANGUAGE == "c" or LANGUAGE == "c4cpp" then
      GENERATED_SOURCES <<
        ".corto/_api.#{EXT}" <<
        ".corto/_wrapper.#{EXT}" <<
        ".corto/_project.#{EXT}" <<
        ".corto/_load.#{EXT}"

      GENERATED_HEADERS ||= [] <<
        "include/_api.h" <<
        "include/_load.h" <<
        "include/_type.h" <<
        "include/_project.h"
    else
      GENERATED_SOURCES <<
        ".corto/_api.#{EXT}" <<
        ".corto/_project.#{EXT}" <<
        ".corto/_load.#{EXT}"

      GENERATED_HEADERS ||= [] <<
        "include/_load.h" <<
        "include/_type.h"
    end

    file "include/_type.h" => [GENFILE, ".corto/packages.txt"] do
      verbose(VERBOSE)
      preload = PP_PRELOAD.join(" ")
      cmd "mkdir -p .corto"
      cmd "touch .corto/_wrapper.#{EXT}"

      localStr = ""
      docStr = ""

      if LOCAL then
        localStr = "--attr local=true"
      else
        begin
          cmd "corto locate corto/gen/doc/doc --silent"
          # docStr = "-g doc/doc"
        rescue
        end
      end

      if defined? PREFIX then
        prefixStr = "--prefix #{PREFIX}"
      end

      command = "valgrind corto pp #{preload} #{GENFILE} --name #{PACKAGE} " +
                "#{PP_SCOPES.map{|s| "--scope " + s}.join(" ")} " +
                "#{PP_OBJECTS.map{|o| "--object " + o}.join(" ")} " +
                "--import #{USE_PACKAGE.join(",")} " +
                "#{PP_ATTR.map{|a| "--attr " + a}.join(" ")} " +
                "#{prefixStr} #{localStr} #{docStr} --lang #{LANGUAGE}"

      if ENV['silent'] != "true" then
        msg "preprocess #{C_NORMAL}#{GENFILE}"
      end
      begin
        cmd command
      rescue
        STDERR.puts "\033[1;31mcorto: command failed: #{command}\033[0;49m"
        if File.exists? "include/_type.h" then
          cmd "rm include/_type.h"
        end
        abort()
      end
    end
    task :default => ["include/_type.h"]
  else
    GENERATED_SOURCES <<
        ".corto/_project.#{EXT}"

    file ".corto/_project.#{EXT}" => [".corto/packages.txt"] do
      verbose(VERBOSE)
      preload = PP_PRELOAD.join(" ")
      cmd "mkdir -p .corto"

      localStr = ""

      if LOCAL then
        localStr = "--attr local=true"
      end
      if LANGUAGE == "c4cpp" then
        langStr = "--attr c4cpp=true"
      elsif LANGUAGE == "cpp" or LANGUAGE == "c++" then
        langStr = "--attr c4cpp=true --attr lang=cpp --attr hpp=include --attr cpp=src"
      end

      command =
        "valgrind corto pp #{preload} #{localStr} --name #{PACKAGE} " +
        "--attr h=include --attr c=src #{PP_ATTR.map{|a| "--attr " + a}.join(" ")} " +
        "--import #{USE_PACKAGE.join(",")} " +
        " -g c/project #{langStr}"

      if ENV['silent'] != "true" then
        msg "preprocess"
      end
      begin
        cmd command
      rescue
        STDERR.puts "\033[1;31mcorto: command failed: #{command}\033[0;49m"
        if File.exists? "include/#{NAME}.h" then
          cmd "rm include/#{NAME}.h"
        end
        abort()
      end
    end
    task :prebuild => [".corto/_project.#{EXT}"]
  end
end

# Document framework integration
task :doc do
  verbose(VERBOSE)
{'fraction_non_alphanumeric': 0.16486605182257355, 'fraction_numerical': 0.007817303469477382, 'mean_word_length': 2.5228960396039604, 'pattern_counts': {'":': 2, '<': 95, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1950072', 'n_tokens_mistral': 4234, 'n_tokens_neox': 4083, 'n_words': 952}