%% Compile Code in Another Development Environment
%
% This example shows how you can relocate generated code and compile it in
% another development environment. Use packNGo to create a zip file that
% contains generated source code and build information. In your target
% development environment, use the codebuild function to build the final
% library or executable file.

% Copyright 2020 The MathWorks, Inc.

%% Configure the Model
% Open the model.
rtwdemo_codebuild

%%
% Do not automatically compile the code or generate a makefile.
set_param('rtwdemo_codebuild', 'GenerateMakefile', 'off');

%%
% After code generation, create a zip file that contains source code and
% build information files.
set_param('rtwdemo_codebuild', 'PackageGeneratedCodeAndArtifacts', 'on');

%% Generate Code
% Run code generation command.
slbuild('rtwdemo_codebuild');

%%
% Check that generated code is packaged as a zip file.
dir rtwdemo_codebuild.zip

%% Switch to Another Development Environment
% This is an optional step. For example, if your current development
% environment is a Linux computer, you can copy the zip file to a Windows
% computer and then perform the subsequent steps on that computer.

%% Unzip Source Code and Build Information Files
% Unzip the files.
rtwdemo_codebuild_files = unzip('rtwdemo_codebuild.zip');

%%
% Identify folder containing unzipped code for the top component.
rtwdemo_codebuild_top_component_folder = fileparts(rtwdemo_codebuild_files{1});

%% Build Executable File Using Default Toolchain
% Identify the default toolchain (based on compiler selected by mex
% -setup).
defaultToolchain = coder.make.getDefaultToolchain;

%%
% Build the executable file.
codebuild(rtwdemo_codebuild_top_component_folder, 'BuildMethod', defaultToolchain);

%%
% Check that executable file is produced.
dir(fullfile(rtwdemo_codebuild_top_component_folder, '..', 'rtwdemo_codebuild*'))

%% Build Executable File Using Default Template Makefile
% Select a template makefile for the current platform.
if ispc
% With ert_vcx64.tmf, you must have Microsoft Visual C++ installed. If
% it is not installed, specify ert_lcc64.tmf to use lcc-win64 instead.
templateMakefile = 'ert_vcx64.tmf';
else
templateMakefile = 'ert_unix.tmf';
end

%%
% Build the executable file.
codebuild(rtwdemo_codebuild_top_component_folder, 'BuildMethod', templateMakefile);

%% Build Shared Library
% On Windows, create a definition file that specifies exported symbols.
if ispc
exportsFile = fullfile...
(rtwdemo_codebuild_top_component_folder, 'rtwdemo_codebuild.def');
fid = fopen(exportsFile, 'w');
fwrite(fid, ['EXPORTS' newline]);
fwrite(fid, ['rtwdemo_codebuild_initialize' newline]);
fwrite(fid, ['rtwdemo_codebuild_step' newline]);
fwrite(fid, ['rtwdemo_codebuild_terminate' newline]);
fclose(fid);
end

%%
% Build the shared library.
codebuild(rtwdemo_codebuild_top_component_folder, 'BuildVariant', 'SHARED_LIBRARY');

%% Build Static Library
codebuild(rtwdemo_codebuild_top_component_folder, 'BuildVariant', 'STATIC_LIBRARY');

%% Create CMake Configuration Files
% Generate a hierarchy of platform and compiler independent CMakeLists.txt
% files
codebuild(rtwdemo_codebuild_top_component_folder, 'BuildMethod', 'CMake');

%%
% With the CMakeLists.txt files, you can use the third-party tool CMake to
% generate makefiles or workspaces for a compiler environment of your
% choice. If CMake is installed on your development computer, build the
% executable file by running these commands in the MATLAB Command Window:
%
% cd(rtwdemo_codebuild_top_component_folder)
%%
% !cmake .
%%
% !cmake --build .