Skip to content

Commit

Permalink
Merge pull request #1443 from STEllAR-GROUP/fixing_1437
Browse files Browse the repository at this point in the history
Always stripping HPX command line arguments before executing start function
  • Loading branch information
sithhell committed Apr 1, 2015
2 parents 2b96931 + 11b1570 commit d80a0e9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
26 changes: 14 additions & 12 deletions hpx/hpx_init_impl.hpp
Expand Up @@ -208,22 +208,24 @@ namespace hpx
return init(f, desc_commandline, argc, argv, empty, empty, mode);
}

/// \cond NOINTERNAL
namespace detail
{
HPX_EXPORT int init_helper(
boost::program_options::variables_map& /*vm*/,
util::function_nonser<int(int, char**)> const& f);
}
/// \endcond

/// \brief Main entry point for launching the HPX runtime system.
///
/// This is a simplified main entry point, which can be used to set up the
/// runtime for an HPX application (the runtime system will be set up in
/// console mode or worker mode depending on the command line settings).
namespace detail
{
inline int init_helper(boost::program_options::variables_map& /*vm*/,
util::function_nonser<int(int, char**)> const& f, int argc, char** argv)
{
return f(argc, argv);
}
}

inline int init(util::function_nonser<int(int, char**)> const& f,
std::string const& /*app_name*/, int argc, char** argv, hpx::runtime_mode mode)
inline int
init(util::function_nonser<int(int, char**)> const& f,
std::string const& /*app_name*/, int argc, char** argv,
hpx::runtime_mode mode)
{
using boost::program_options::options_description;
options_description desc_commandline(
Expand All @@ -233,7 +235,7 @@ namespace hpx
util::function_nonser<void()> const empty;

return init(
util::bind(detail::init_helper, util::placeholders::_1, f, argc, argv),
util::bind(detail::init_helper, util::placeholders::_1, f),
desc_commandline, argc, argv, cfg, empty, empty, mode);
}
}
Expand Down
41 changes: 41 additions & 0 deletions src/hpx_init.cpp
Expand Up @@ -1296,5 +1296,46 @@ namespace hpx

return result;
}

namespace detail
{
HPX_EXPORT int init_helper(
boost::program_options::variables_map& /*vm*/,
util::function_nonser<int(int, char**)> const& f)
{
hpx::util::section const& ini = hpx::get_runtime().get_config();
std::string cmdline(ini.get_entry("hpx.reconstructed_cmd_line", ""));

using namespace boost::program_options;
#if defined(BOOST_WINDOWS)
std::vector<std::string> args = split_winmain(cmdline);
#else
std::vector<std::string> args = split_unix(cmdline);
#endif

// Copy all arguments which are not hpx related to a temporary array
boost::scoped_array<char*> argv(new char*[args.size()+1]);
std::size_t argcount = 0;
for (std::size_t i = 0; i != args.size(); ++i)
{
if (0 != args[i].find("--hpx:")) {
argv[argcount++] = const_cast<char*>(args[i].data());
}
else if (6 == args[i].find("positional", 6)) {
std::string::size_type p = args[i].find_first_of("=");
if (p != std::string::npos) {
args[i] = args[i].substr(p+1);
argv[argcount++] = const_cast<char*>(args[i].data());
}
}
}

// add a single nullptr in the end as some application rely on that
argv[argcount] = 0;

// Invoke custom startup functions
return f(static_cast<int>(argcount), argv.get());
}
}
}

2 changes: 2 additions & 0 deletions tests/regressions/CMakeLists.txt
Expand Up @@ -28,11 +28,13 @@ foreach(subdir ${subdirs})
endforeach()

set(tests
commandline_options_1437
id_type_ref_counting_1032
multiple_init
unhandled_exception_582
)

set(commandline_options_1437_PARAMETERS THREADS_PER_LOCALITY 2)
set(id_type_ref_counting_1032_PARAMETERS THREADS_PER_LOCALITY 1)
set(unhandled_exception_582_PARAMETERS THREADS_PER_LOCALITY 1)

Expand Down
31 changes: 31 additions & 0 deletions tests/regressions/commandline_options_1437.cpp
@@ -0,0 +1,31 @@
// Copyright (c) 2015 Andreas Schaefer`
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Demonstrating #1437: hpx::init() should strip HPX-related flags from argv

#include <hpx/hpx.hpp>
#include <hpx/hpx_init.hpp>
#include <hpx/util/lightweight_test.hpp>

bool invoked_main = false;

int my_hpx_main(int argc, char **argv)
{
// all HPX command line arguments should have been stripped here
HPX_TEST(argc == 1);

invoked_main = true;
return hpx::finalize();
}

int main(int argc, char **argv)
{
HPX_TEST(argc > 1);

HPX_TEST_EQ(hpx::init(&my_hpx_main, "testapp", argc, argv), 0);
HPX_TEST(invoked_main);

return hpx::util::report_errors();
}

0 comments on commit d80a0e9

Please sign in to comment.