function options = getBuildConfigOrderedOptions(h)

%   Copyright 2014 The MathWorks, Inc.

if ~any( strcmpi( h.SupportedLanguages, coder.make.ToolchainInfo.getSupportedLanguages() ) )
    assert( false, 'ToolchainInfo is not configured for toolchains that support non-C/C++ language.' );
end

options = [  ];
% Baseline build config options first
bt = [h.BuildTools h.PrebuildTools h.PostbuildTools];
for idx = 1:numel( bt )
    btNames = bt( idx ).keys();
    for i = 1:numel( btNames )
        btInfo = bt( idx ).getValue( btNames{ i } );
        options = horzcat( options, btInfo.OptionsRegistry( 1:2:end ) );  %#ok<AGROW>
    end
end

% Put build config options added by the user last
config = h.getBuildConfiguration();
all = config.Options.keys;
remaining = setdiff( all, options );
if ~isempty( remaining )
    options = horzcat( options, remaining );
end
options = unique(options, 'stable');
end
