########################################################################
##
## Copyright (C) 2026 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn {} {@var{status} =} __mcc_compile__ (@var{cpp_file}, @var{deps}, @var{output_name}, @var{verbose})
## Compile C++ wrapper to standalone executable using mkoctfile.
##
## This is an internal function used by @code{mcc}.
## @end deftypefn

function status = __mcc_compile__ (cpp_file, deps, output_name, verbose)

  ## Get Octave installation or build directory paths for include directories
  ## Try to find the source/build directory by looking for liboctave
  octave_home = OCTAVE_HOME ();
  build_dir = "";
  
  ## Check if we're in a build tree (liboctave exists)
  mcc_file = which ("mcc");
  possible_dirs = {pwd(), octave_home};
  if (! isempty (mcc_file))
    mcc_dir = fileparts (mcc_file);
    if (! isempty (mcc_dir))
      possible_dirs{end+1} = fileparts (mcc_dir);  # scripts/miscellaneous -> scripts -> root
      possible_dirs{end+1} = fileparts (fileparts (mcc_dir));  # scripts -> root
    endif
  endif
  
  for i = 1:length (possible_dirs)
    test_dir = possible_dirs{i};
    if (! isempty (test_dir) && exist (fullfile (test_dir, "liboctave"), "dir"))
      build_dir = test_dir;
      break;
    endif
  endfor
  
  ## Build mkoctfile command
  cmd_args = {"--link-stand-alone", cpp_file, "-o", output_name};
  
  ## Add include directories if we found a build tree
  ## mkoctfile expects -I/path format (no space)
  ## Add all subdirectories that might contain headers
  if (! isempty (build_dir))
    build_root_inc = build_dir;  # For octave-config.h in root
    
    ## Add root first
    cmd_args{end+1} = sprintf ("-I%s", build_root_inc);
    
    ## Add all liboctave subdirectories
    liboctave_dirs = {"liboctave"; "liboctave/array"; "liboctave/operators";
                      "liboctave/numeric"; "liboctave/system"; "liboctave/util";
                      "liboctave/wrappers"};
    for i = 1:length (liboctave_dirs)
      dir_path = fullfile (build_dir, liboctave_dirs{i});
      if (exist (dir_path, "dir"))
        cmd_args{end+1} = sprintf ("-I%s", dir_path);
      endif
    endfor
    
    ## Add all libinterp subdirectories
    libinterp_dirs = {"libinterp"; "libinterp/corefcn"; "libinterp/octave-value";
                      "libinterp/template-inst"; "libinterp/parse-tree";
                      "libinterp/operators"; "libinterp/pt-eval"};
    for i = 1:length (libinterp_dirs)
      dir_path = fullfile (build_dir, libinterp_dirs{i});
      if (exist (dir_path, "dir"))
        cmd_args{end+1} = sprintf ("-I%s", dir_path);
      endif
    endfor
    
    ## Add library paths for build tree
    liboctave_lib = fullfile (build_dir, "liboctave", ".libs");
    libinterp_lib = fullfile (build_dir, "libinterp", ".libs");
    libgnu_lib = fullfile (build_dir, "libgnu", ".libs");
    
    if (exist (liboctave_lib, "dir"))
      cmd_args{end+1} = sprintf ("-L%s", liboctave_lib);
    endif
    if (exist (libinterp_lib, "dir"))
      cmd_args{end+1} = sprintf ("-L%s", libinterp_lib);
    endif
    if (exist (libgnu_lib, "dir"))
      cmd_args{end+1} = sprintf ("-L%s", libgnu_lib);
    endif
  endif
  
  if (verbose)
    cmd_args{end+1} = "-v";
  endif

  ## Compile using mkoctfile
  if (verbose)
    printf ("mcc: Running: mkoctfile %s\n", strjoin (cmd_args, " "));
  endif

  [output, status] = mkoctfile (cmd_args{:});

  if (verbose && ! isempty (output))
    printf ("%s", output);
  endif

  if (status != 0)
    error ("__mcc_compile__: mkoctfile compilation failed");
  endif

  ## Note: The .m files are loaded at runtime by the generated C++ code
  ## using the 'run' function. For a more complete solution, we could:
  ## 1. Embed .m files as strings in the C++ code
  ## 2. Create a resource bundle
  ## 3. Package them alongside the executable
  ##
  ## For now, the generated executable will look for .m files in the
  ## current directory or on the Octave path.

  if (verbose)
    printf ("mcc: Standalone executable '%s' created successfully\n", output_name);
    printf ("mcc: Note: .m files must be accessible at runtime\n");
  endif

endfunction










