/*----------------------------------------------------------------------------
 *
 *  \file   writerCMake.cpp
 *
 *  \brief  writes cmake script file with board properties
 *
 *          If the existing file does not differ from the new one, the file
 *          is not touched at all.
 *          This will prevent cmake to go crazy when the file keeps being
 *          touched. So the unchanged-logic does not have to be implemented in
 *          the CMake-Scripts.
 *
 *  \date   20210813
 *  \author Maximilian Seesslen <mes@seesslen.net>
 *
 *---------------------------------------------------------------------------*/


//---Documentation------------------------------------------------------------


//---Defines------------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#include <QFile>
#include <QDebug>

//---Own------------------------------

#include "writerLinkerScript.h"
#include "board.h"
#include "app.h"

//--- Implementation ----------------------------------------------------------


CWriterLinkerScript::CWriterLinkerScript(CPinmux &pinmux, const QString basePath)
   :CWriter(pinmux, basePath)
{
   if( !basePath.isEmpty() )
   {
      QFile file( getBasePath() + "arena_generated.ld" );
      //qWarning() << ">>> remove: " << getBasePath() + "arena_generated.ld";
      file.remove();
   }
   return;
}


int CWriterLinkerScript::writeLinkerScript( )
{
   QFile file( getBasePath() + "arena_generated.ld" );

   if (!file.open(QFile::WriteOnly | QFile::Truncate))
      qFatal("Could not open header file for writing");
   QTextStream out(&file);

   writeCHeader( out );
   out << "\n";

   out << "/*\n";
   out << " *  Number of total properties: " << board().getProperties().count() << "\n";
   out << " */\n";
   out << "\n";

   QVariantMap properties;
   #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
      properties.unite( board().getProperties() );
      properties.unite( app().getProperties() );
   #else
      properties.insert( board().getProperties() );
      properties.insert( app().getProperties() );
   #endif
   
   if ( properties.count() )
   {
      for(const auto &property: properties.keys() )
      {
         const char *category="linker.";
         if(property.startsWith( category ))
         {
            QString _property=property;
            _property.remove(0, strlen( category ) );
            out << _property << " = " << properties.value(property).toString() << ";\n";
         }
      }
   }

   out << "\n";
   if( !board().getProperties().contains("baseLinkerScript") )
   {
      qWarning("Board name: %s", qPrintable( board().getName() ));
      qFatal("Board JSON files do not contain property \"baseLinkerScript\". You need that property.");
   }
   QString baseLinkerScript=board().getProperties()["baseLinkerScript"].toString();
   if( ! baseLinkerScript.isEmpty() )
   {
      out << "INCLUDE \"" << baseLinkerScript << "\";\n";
   }

   out << "\n";
   out << "\n";

   out << "/*---fin----------------------------------------------------------------------*/\n";
   file.close();

   // The cmake writer needs filename of the  linker script. No path needed because path for generated
   // files may get moved
   //pinmux().getBoard()->addProperty("cmake.ARENA_LINKERSCRIPT", /* getBasePath() + */ "arena_generated.ld" );

   return(0);
}

int CWriterLinkerScript::writeFiles( ) /* virtual override */
{
   return(0);
}

//---fin----------------------------------------------------------------------
