Donald Trump, well on his way to clinching the Republican presidential nomination, was ruthless in his crude attacks on rival Ted Cruz. He repeatedly mocked the Texas senator as “Lyin’ Ted,” suggested his father played a role in the assassination of John F. Kennedy, and even made fun of the appearance of Cruz’s wife, Heidi.

“I don’t get angry often, but you mess with my wife, you mess with my kids, that’ll do it every time,” Cruz told reporters at a campaign stop in March 2016, jabbing his finger angrily at the cameras. “Donald, you’re a sniveling coward, and leave Heidi the hell alone.”

Two-and-a-half years later, Cruz will take the stage at Toyota Center in Houston with Trump, who is holding a rally to help Cruz in his surprisingly close reelection battle with Rep. Beto O’Rourke, D-Texas.

The event is the culmination of a tempestuous, on-again-off-again relationship between the two men, as Cruz has morphed from a fierce critic who refused to endorse Trump at the 2016 Republican convention to a servile ally who now speaks eagerly of working with President trump to deliver on “our promises.”

Their rocky journey from nomance to bromance hinges largely on political expediency — a shared recognition that both benefit more from an alliance than a running feud.

“Look, they went to counseling, things are OK now, they’re back together, they’re getting along now,” said Rick Tyler, an MSNBC political analyst and Republican strategist who worked as a spokesman for Cruz’s 2016 presidential bid.

But, he added, “I still don’t know that they have a relationship in the way that you would think of two people that are friends. They have a transactional relationship, and it’s working for now.”

In the Senate, where Trump holds a razor-thin Republican majority, the president appreciates that Cruz has emerged as a steadfast ally on a range of critical issues, from trying to repeal former president Barack Obama’s heath-care law to passing tax cuts to pushing through conservative judicial nominees.

And Cruz understands that, especially in a state like Texas, bucking the sitting Republican president — however controversial he is — is not what most of his supporters want. Trump also has the ability to offer a boost in his Senate race, which has emerged as one of the most hotly watched in the country.

“I think that Ted accepted his defeat and realized he had a choice to make — continue the fight or work with Trump and he chose to work with Trump,” said Sen. Lindsey Graham, R-S.C. “So it’s in Ted’s interest to be helpful to the president, and it’s in the president’s interest to have allies when you have 51 Senate seats, and I think that’s what happened here.”

Yet when the two were battling for the nomination, the duo laced into one another with gleeful and memorable vitriol. Trump called Cruz “a little bit of a maniac,” “a totally unstable individual,” and “the single biggest liar” he had ever encountered. Cruz countered by dubbing Trump “utterly amoral,” “a serial philanderer,” a “pathological liar” and “a braggadocios arrogant buffoon.”

The relationship, however, was not always contentious. Cruz and a few aides traveled to Trump Tower in 2015 for a cordial visit with Trump. And early in the campaign, Cruz’s team made a strategic decision not to attack Trump in the hopes that the Texas senator would emerge as the last candidate standing against him. Cruz invited Trump to a rally at the Capitol against the Iran nuclear deal and even worked to organize a never-realized joint trip with Trump to the southern border.

But then things went south — a turn so acrimonious that reconciliation, when it came, was haltingly slow.

The nadir came at the Republican convention in Cleveland. After Trump won his party’s nomination, he asked Cruz to speak at the convention and endorse him.

With feelings raw, Cruz — who still commanded the loyalty of a swath of conservative and evangelical voters — consulted with a small group of loyal aides and family members. Some counseled not speaking at the convention at all, while others said that if he did appear, he needed to be prepared to endorse the party’s nominee, according to current and former advisers. But Cruz, with the support of some in his orbit, alighted on a third option: He would accept the coveted speaking slot but refuse to endorse Trump.

Cruz knew the decision was contentious and privately rehearsed a range of scenarios, advisers said, from getting heckled to Trump coming out on stage to accept an endorsement that was never offered.

The moment — which produced a roar of jeering and boos and Trump’s surprise arrival in the convention hall — unnerved even some in Cruz’s orbit. Many of his own donors excoriated him, including megadonors Robert and Rebekah Mercer, a reclusive father-daughter duo who took the rare step of issuing a public statement criticizing him.

The Cruz team — who say they sent an advance copy of his speech to the Trump campaign — felt set up to fail, while the Trump team felt burned by Cruz.

“It felt like it was Ohio State walking into Michigan stadium,” said a Cruz adviser, speaking anonymously, to share a candid recollection. “This was intense. This was deep. It wasn’t comfortable and it wasn’t fun and we went back and regrouped.”

Finally, after weeks of private discussions between the two sides, Cruz endorsed Trump in a Facebook post at the end of September 2016. In his post, he called Hillary Clinton, the Democratic nominee, “wholly unacceptable” and touted the “very strong list of potential Supreme Court nominees” Trump had just released.

Most notably, the list — from which Trump had explicitly promised to choose to fill his first Supreme Court vacancy — included Sen. Mike Lee, R-Utah, who is close friends with Cruz and who Cruz had pushed to be included as a key sticking point for his endorsement.

The Facebook endorsement came two weeks before the “Access Hollywood” tape emerged, featuring Trump bragging and speaking lewdly about kissing and groping women without their consent. But Cruz remained loyal to Trump, a moment the president’s allies have not forgotten.

“There was a lot of fear throughout the campaign that he was going to pull his support,” said Andy Surabian, who ran the war room during Trump’s campaign and now serves as a political adviser to Trump’s oldest son, Donald Trump Jr. “When he didn’t do what so many other Republicans did, even though he’d had a fraught relationship with Trump because of their battles during the primaries, that’s the moment when the way Ted Cruz was viewed in Trumpworld began to change for the better.”

By the time Trump won the presidency, Cruz was prepared to work with him. Shortly after Election Day, Cruz convened a meeting with his Senate staff and offered a stark message of support for the new president, people familiar with the discussion said.

The Texas senator rattled off a number of his key priorities — moving the U.S. Embassy in Israel, withdrawing from the Iran deal, passing tax cuts, repealing Obama’s health-care law, appointing conservative judges to the bench — and talked about working with Trump to promote a shared agenda.

“We can do everything we set out to do — just in this case it will be President Trump signing most of these bills,” Cruz told his staff.

“One thing that presidents of both parties figure out pretty quickly is that who’s helpful and who’s not in Congress is different than who they might have thought,” said Josh Holmes, a Republican strategist who previously served as chief of staff to Senate Majority Leader Mitch McConnell, R-Ky. “And it’s hard to make the argument that Cruz has been anything but helpful to Donald Trump since he became president.”

There were personal gestures as well. Cruz visited the president-elect at Trump Tower shortly after his election for a warm meeting that stretched on longer than expected. The Trumps invited the Cruzes, and their two young daughters, to dinner at the White House, and the Cruzes also dined with Trump’s daughter, Ivanka, and son-in-law, Jared Kushner, at a corner table at Fiola Mare in Washington. Kushner joined Cruz for one of his weekly pickup basketball games in a gym in the Dirksen Senate Office Building.

Another bonding moment came when, shortly after the election, Cruz and Trump Jr. were seated together during a think tank dinner. The two hit it off — one Cruz adviser said the seating arrangement was slyly intentional — and Trump Jr. invited the senator back to the lobby of the Trump International Hotel in Washington for drinks. The two stayed late into the evening, and Trump Jr. later told friends he knew it was not easy for Cruz to join him there.

When Trump Jr. and his team noticed the polls tightening in Cruz’s Senate race, they reached out to his campaign and offered to help. Earlier this month, Trump Jr. traveled to Texas for a full day of events with Cruz: three fundraisers and two rallies.

And on Monday, the president will also appear alongside Cruz. Like many makeup stories, this one has competing versions about who made the first move. Both sides claim the other reached out first, and there has been some light grumbling. Some in Trump’s orbit believe he is doing Cruz a favor — “It’s the middle of October,” said a senior White House official. “Do we really want to be spending our time in Texas?” — while least one Cruz ally surmised that Trump wants to claim credit now that it appears likely Cruz will win.

There are signs that Cruz’s detente with Trump has helped improve some of this other relationships, as well. Graham, who has emerged as one of the president’s key allies, once quipped that Cruz was so unpopular that if he was murdered on the Senate floor, none of his colleagues would vote to convict.

“Well I would amend that now — I would vote to convict,” Graham said. “I like Ted. It’d be a real loss to the cause.”

Ahead of their rally, however, Cruz still sounds a bit hesitant about Trump — declining Sunday to say whether he views him as friend or foe.

“He’s the president. I work with the president in delivering on our promises,” Cruz said in an interview on ABC News’s “This Week.” “What I told the president the week after the election, I said, ‘Mr. President, I want to do everything humanly possible to roll up my sleeves and lead the fight in the Senate to deliver on our promises.’ “

More in Politics

The White House's move to restore Acosta's pass, announced in a letter to the news network, appeared to be a capitulation to CNN in its brief legal fight against the administration. White House officials had suspended Acosta's White House press pass following a contentious news conference on Nov. 7, prompting CNN to sue last week to force the administration to...

Not only was racial animus a likely factor when Charter Communications repeatedly rejected negotiations with Entertainment Studios, the TV programmer, but Charter's attempt to shield itself from allegations of bias using the First Amendment is also without merit, according to the U.S. Court of Appeals for the Ninth Circuit.